2014-02-20 93 views
0

我正在開發應用程序內購買項目:以下是應用程序內購買代碼。當我點擊「購買物品」按鈕谷歌在應用程序購買對話框顯示和成功付款後,我得到「付款是成功的」。在應用程序購買中禁用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); 
     } 
    } 
} 

回答

0

我覺得你購買的結果上onActivityResult,所以在代碼中設置點擊您的按鈕,虛假與下面的代碼:

purchaseBtn.setEnabled(false); 

你可以改變你的按鈕的背景,這使得更好的視覺效果,如灰色輸出

所以你的代碼必須如下所示:

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(); 

      // added this two line ///// 

      purchaseBtn.setEnabled(false); 
      purchaseBtn.setBackGroundColor(Color.GRAY); 


      } catch (JSONException e) { 
       System.out.println("Failed to parse purchase data."); 
       e.printStackTrace(); 
      } 
     } 

並且定義了purchaseBtn你可以在你的類的頂端找到這個方法。

,如果你想爲你需要保存一個標誌,以分享偏好或數據庫或從互聯網上檢查所有的時間關閉按鈕,我寫了SP代碼對你:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.putBoolean("isPurchase",true); 
    editor.commit(); 

onCreate方法需要拿到檢查值:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(etApplicationContext()); 
    boolean isPurchase = preferences.getBoolean("isPurchase",false); 
    if(isPurchase) 
    { 
    // disable your button 
    } 
+0

謝謝先生的回覆。現在,如果我再次打開此應用程序,我會禁用此按鈕? –

+0

看到我的編輯禁用後重新打開應用程序 –

0

「購買物品」 按鈕自動禁用當我打開這個應用程序

意味着你需要首先知道項目已購買或不禁用或啓用購買button.For這種使用mService.getPurchases這回你已經被用戶購買的所有物品:

Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null); 

現在,如果項目已經存在在ownedItems然後禁用購買按鈕,否則啓用它。

欲瞭解更多信息,請參閱Querying for Purchased Items

+0

我已經加入 \t \t嘗試{ \t \t \t捆綁ownedItems = mservice.getPurchases(3 getPackageName(), \t \t \t \t \t 「應用程式內」,NULL); \t \t \t int response = ownedItems.getInt(「RESPONSE_CODE」); \t \t \t如果(響應== 0){ \t \t \t \t purchaseBtn。的setEnabled(假); \t \t \t} \t \t}趕上(RemoteException的E1){ \t \t \t // TODO自動生成的catch程序塊 \t \t \t e1.printStackTrace(); \t \t} onCreate()方法。現在我無法朗姆酒申請。我得到錯誤。 –

+0

@AnandSingh:你在哪?並嘗試爲'for(String key:ownedItems.keySet()){ Object value = ownedItems.get(key); Log.d(TAG,String.format(「%s%s(%s)」,key, value.toString(),value.getClass()。getName())); }' –