我正在我的项目中实施应用内购买。购买工作正常,但我想在购买完成后打印吐司消息。
现在即使未完成购买或出现错误,也会显示消息。我尝试了很多方法但没有成功。 这是我的代码:
ArrayList<String> skuList = new ArrayList<String> ();
skuList.add(ITEM_SKU);
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
try {
Bundle skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);
int response = skuDetails.getInt("RESPONSE_CODE");
if (response == BILLING_RESPONSE_RESULT_OK) {
ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
for (String thisResponse : responseList) {
JSONObject object = new JSONObject(thisResponse);
String sku = object.getString("productId");
String price = object.getString("price");
if (sku.equals(ITEM_SKU)) mPremiumUpgradePrice = price;
Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), sku, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
int endingResponse = skuDetails.getInt("RESPONSE_CODE");
//restorePurchasedItems();
SharedPrefHelper.getInstance(getApplication()).savePurchaseStatus(true);
if(pendingIntent != null) {
startIntentSenderForResult(pendingIntent.getIntentSender(), PURCHASE_REQUEST_CODE, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
restorePurchasedItems();
}//if(response == 0)
else {
Toast.makeText(mContext, "Item is already owned...", Toast.LENGTH_SHORT).show();
}
}//for(String thisResponse : responseList)
}//if
} catch (RemoteException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
请帮帮我。提前致谢。
答案 0 :(得分:1)
检查此链接 https://developer.android.com/google/play/billing/billing_reference.html
如果有条件的话,请把你的祝酒。
int response = ownedItems.getInt("RESPONSE_CODE");
if (response == BILLING_RESPONSE_RESULT_OK) {
答案 1 :(得分:0)
通过调用onActivityResult()回调方法解决了这个问题。
答案 2 :(得分:0)
这可能会有所帮助。
Toast toast = Toast.makeText(this, "CORRECT!", Toast.LENGTH_LONG);
View toastView = toast.getView();
TextView viewText = (TextView) toastView.findViewById(android.R.id.message);
viewText.setTextColor(getResources().getColorStateList(R.color.colorSuccessToast));
toast.show();