今天,我已根据文档在应用计费版本3中实施了Google。我需要多次购买被管理产品,因此我每次购买时都会编写代码来使用产品。
我的代码适用于测试偏差但不适用于实际偏斜。 " PendingIntent"对象每次第二次返回null我尝试购买以前购买的产品您对我的代码有什么建议吗?
IInAppBillingService mService;
ServiceConnection mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
@Override
public void onServiceConnected(ComponentName name,
IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
}
/////////////// on purchase button click event///////////
try {
Bundle buyIntentBundle =
mService.getBuyIntent(3, getPackageName(),
skew, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
startIntentSenderForResult(pendingIntent.getIntentSender(),
1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),Integer.valueO(0));
String purchaseToken = "inapp:"+getPackageName()+":"+skew;
Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/////////////// on purchase button click event///////////
// consuming purchase insie onActivityResult function ////
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001) {
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
if (resultCode == RESULT_OK) {
try {
JSONObject jo = new JSONObject(purchaseData);
String token=jo.getString("purchaseToken");
// consuming here
int response = mService.consumePurchase(3, getPackageName(), token);
// some database operation
}
catch (Exception e) {
// alert("Failed to parse purchase data.");
e.printStackTrace();
}
}
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mService != null) {
unbindService(mServiceConn);
}
}