Reputation: 17
I want to add buttons in my App which start an In-App Purchase and after succesfully paid it should perform an action.
What i did already...
I added buttons to the layout ,which got IDs like "doc_link1" to "doc_link11".
Question: Each button should cause an In - App Purchase and after paid should before an action. I already set up my In-App-Purchase Account and IDs in the Google Play Console.
How can I add those In-App-Purchase IDs to the buttons successfully, that when a user presses the button it performs the In-App-Purchase?
package com.test.keyboard;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class LinksFragment extends Fragment implements View.OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.roko_mojis_p2, null);
view.findViewById(R.id.doc_link_1).setOnClickListener(this);
view.findViewById(R.id.doc_link_2).setOnClickListener(this);
view.findViewById(R.id.doc_link_4).setOnClickListener(this);
view.findViewById(R.id.doc_link_5).setOnClickListener(this);
view.findViewById(R.id.doc_link_6).setOnClickListener(this);
view.findViewById(R.id.doc_link_7).setOnClickListener(this);
view.findViewById(R.id.doc_link_8).setOnClickListener(this);
view.findViewById(R.id.doc_link_9).setOnClickListener(this);
view.findViewById(R.id.doc_link_10).setOnClickListener(this);
view.findViewById(R.id.doc_link_11).setOnClickListener(this);
return view;
}
private void sendMail() {
String mailto = "mailto:" + getResources().getString(R.string.email_mailto) +
"?subject=" + Uri.encode(getResources().getString(R.string.email_subject)) +
"&body=" + Uri.encode("");
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse(mailto));
startActivity(Intent.createChooser(emailIntent, "Send Email"));
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.doc_link_1:
// About Me
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_2:
// How to install
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_4:
// Purchase Pack 1
sendMail();
break;
case R.id.doc_link_5:
// Purchase Pack 2
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_6:
// Purchase Pack 3
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_7:
// Purchase Pack 4
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_8:
// Purchase Pack 5
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_9:
// Purchase Pack 6
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_10:
// Purchase Pack 7
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_11:
// Purchase Pack 8
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
default:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
}
}
}
Upvotes: 1
Views: 482
Reputation: 7749
If you're using googles in-app billing library, you can do it like this:
case R.id.doc_link_1:
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSku(skuId)
.setType(SkuType.INAPP) // SkuType.SUB for subscription
.build();
int responseCode = mBillingClient.launchBillingFlow(flowParams);
See https://developer.android.com/google/play/billing/billing_library_overview#Enable
Upvotes: 1