我正在開發應用程序內購買項目:以下是應用程序內購買代碼。當我點擊「購買物品」按鈕谷歌在應用程序購買對話框顯示和成功付款後,我得到「付款是成功的」。在應用程序購買中禁用Android中的「購買」按鈕活動
現在我想在付款成功時填寫「購買物品」按鈕當我打開此應用程序時自動禁用。
這裏是MainActivity
代碼:
public class MainActivity extends Activity {
IInAppBillingService mservice;
ServiceConnection connection;
String inappid = "android.test.purchased"; // replace this with your in-app
// product id
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.purchase);
connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mservice = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mservice = IInAppBillingService.Stub.asInterface(service);
}
};
bindService(new Intent(
"com.android.vending.billing.InAppBillingService.BIND"),
connection, Context.BIND_AUTO_CREATE);
Button purchaseBtn = (Button) findViewById(R.id.purchase);
purchaseBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ArrayList skuList = new ArrayList();
skuList.add(inappid);
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
Bundle skuDetails;
try {
skuDetails = mservice.getSkuDetails(3, getPackageName(),
"inapp", querySkus);
int response = skuDetails.getInt("RESPONSE_CODE");
if (response == 0) {
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(inappid)) {
System.out.println("price " + price);
Bundle buyIntentBundle = mservice.getBuyIntent(3, getPackageName(), sku, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
}
}
}
}
catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SendIntentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@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 sku = jo.getString(inappid);
Toast.makeText(MainActivity.this, "You have bought the " + sku + ". Excellent choice,adventurer!", Toast.LENGTH_LONG).show();
}
catch (JSONException e) {
System.out.println("Failed to parse purchase data.");
e.printStackTrace();
}
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (connection != null) {
unbindService(connection);
}
}
}
謝謝先生的回覆。現在,如果我再次打開此應用程序,我會禁用此按鈕? –
看到我的編輯禁用後重新打開應用程序 –