我在使用Anjlab应用内结算库的Android应用中有一个“购买”按钮。购买成功后,我想向服务器发送请求,服务器随后将通知我。
但是,我根本无法使用“ onProductPurchased”功能。经过研究,我发现我需要调用onActivityResult。但是,即使那样也没有得到。
获得onActivityResult的解决方案之一是对其进行覆盖,但仍然行不通。请找到下面的代码,并建议我需要做些什么,以便一旦购买成功,我就可以致电后端。
public class QuestionPrice extends Fragment implements View.OnClickListener, BillingProcessor.IBillingHandler{
private static final String PRODUCT_ID = "prod_00001";
private static final String PRODUCT_ID2 = "prod_00002";
private int paymentmade;
private String paymentdone;
ArrayList<String> arrayListOfProductIds;
private static final String LICENSE_KEY = "PUT YOUR MERCHANT KEY HERE";
private static final String MERCHANT_ID=null;
private static final String TAG = "Askquest";
private BillingProcessor bp;
private boolean readyToPurchase = false;
Button buybutton;
Button buybuttonReport;
private boolean purchased;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_question_price, container, false);
buybutton = (Button) view.findViewById(R.id.buybutton);
buybuttonReport = (Button) view.findViewById(R.id.buybuttonReport);
buybutton.setOnClickListener(this);
buybuttonReport.setOnClickListener(this);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if(!BillingProcessor.isIabServiceAvailable(getActivity())) {
Toast.makeText(getActivity(), "In-app billing service is unavailable, please upgrade Android Market/Play to version >= 3.9.16", Toast.LENGTH_SHORT).show();
}
bp = new BillingProcessor(getActivity(), LICENSE_KEY, MERCHANT_ID,this);
SkuDetails skuDetails = bp.getPurchaseListingDetails("PRODUCT_ID");
getActivity().setTitle("Products");
}
@Override
public void onClick(View v) {
SharedPreferences msharedprefs = getActivity().getSharedPreferences("sharedprefdata", MODE_PRIVATE);
SharedPreferences.Editor editor = msharedprefs.edit();
switch (v.getId()) {
case R.id.buybutton:
if (bp.loadOwnedPurchasesFromGoogle() && bp.isPurchased(PRODUCT_ID)) {
boolean loadOwnedPurchasesFromGoogle = bp.loadOwnedPurchasesFromGoogle();
boolean isPurchased = bp.isPurchased(PRODUCT_ID);
TransactionDetails premiumTransactionDetailss = bp.getPurchaseTransactionDetails(PRODUCT_ID);
Log.i("Transaction Details ", " TT Details " + premiumTransactionDetailss);
if (purchased) {
Toast.makeText(getActivity(), "You have already purchased a question.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Purchase Initiated", Toast.LENGTH_SHORT).show();
bp.purchase(getActivity(), PRODUCT_ID);
TransactionDetails premiumTransactionDetails2 = bp.getPurchaseTransactionDetails(PRODUCT_ID);
purchased = bp.isPurchased(PRODUCT_ID);
Log.i("TAG", "isPurchased" + purchased);
if (purchased) {
Log.i("TAG", "Congratulations!!! Purchase Successfull");
Toast.makeText(getActivity(), "Congratulations!!! Purchase Successfull", Toast.LENGTH_SHORT).show();
}
}
}
break;
case R.id.buybuttonReport:
TransactionDetails premiumTransactionDetails2 = bp.getPurchaseTransactionDetails(PRODUCT_ID2);
Log.i("Transaction Details ", " TT Details "+premiumTransactionDetails2);
if (bp.loadOwnedPurchasesFromGoogle() && bp.isPurchased(PRODUCT_ID2))
{
boolean loadOwnedPurchasesFromGoogle = bp.loadOwnedPurchasesFromGoogle();
boolean isPurchased = bp.isPurchased(PRODUCT_ID2);
TransactionDetails premiumTransactionDetailss = bp.getPurchaseTransactionDetails(PRODUCT_ID2);
Log.i("Transaction Details ", " TT Details "+premiumTransactionDetailss);
Log.i("TAG", ""+msharedprefs.getInt("reportQues", -1));
Toast.makeText(getActivity(), "You have already purchased a report.", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getActivity(), "Purchase Initiated", Toast.LENGTH_SHORT).show();
bp.purchase(getActivity(), PRODUCT_ID2);
purchased = bp.isPurchased(PRODUCT_ID2);
if ( purchased)
{
Log.i("TxnDetails : ","TxnDetails : "+premiumTransactionDetails22.purchaseInfo.purchaseData.orderId.toString());
}
Log.i("TAG", "isPurchased"+purchased );
}
break;
}
}
@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
SharedPreferences msharedprefs = getActivity().getSharedPreferences("sharedprefdata", MODE_PRIVATE);
SharedPreferences.Editor editor = msharedprefs.edit();
String userid = msharedprefs.getString("userid", null);
String username = msharedprefs.getString("username", null);
Toast.makeText(getActivity(), "On Product Purchased", Toast.LENGTH_SHORT).show();
Log.i("TAG", "On Product Purchased" );
purchased = bp.isPurchased(PRODUCT_ID2);
if ( purchased) {
Toast.makeText(getActivity(), "Congratulations!!! Purchase Successfull", Toast.LENGTH_SHORT).show();
Log.i("TAG", "Button Clicked Product Purchased" );
Toast.makeText(getActivity(), "Button Clicked Product Purchased", Toast.LENGTH_SHORT).show();
}
editor.putInt("paymentdone", 1);
editor.commit();
purchasemade(userid);
}
@Override
public void onPurchaseHistoryRestored() {
Log.i("TAG", "Button Clicked Product History Restored" );
Toast.makeText(getActivity(), "Button Clicked Product History Restored", Toast.LENGTH_SHORT).show();
editor.putInt("paymentdone", 0);
editor.commit();
}
@Override
public void onBillingError(int errorCode, @Nullable Throwable error) {
Toast.makeText(getActivity(), "Button Clicked Billing Error", Toast.LENGTH_SHORT).show();
Log.i("TAG", "Button Clicked Billing Error" );
editor.putInt("paymentdone", 0);
editor.commit();
}
@Override
public void onBillingInitialized() {
Toast.makeText(getActivity(), "Billing Initialized", Toast.LENGTH_SHORT).show();
readyToPurchase = true;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!bp.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag("Products");
Toast.makeText(getActivity(), "Inside On Activity Result", Toast.LENGTH_SHORT).show();
Log.i("TAG", "Inside On Activity Result" );
if (fragment != null)
{
((QuestionPrice)fragment).onActivityResult(requestCode, resultCode, data);
}
}
}
@Override
public void onDestroy() {
if (bp != null) {
bp.release();
}
super.onDestroy();
}
private void purchasemade(String userid) {
Log.i("TAG", "Inside Purchase Made" );
Toast.makeText(getActivity(), "Inside Purchase Made", Toast.LENGTH_SHORT).show();
}
}
我尝试了SO中发布的其他类似问题的一些步骤,但没有一个起作用。