2013-01-09 90 views
0

後,我在應用程序中實施了Google。它發佈應用程序後工作正常。但我面臨一個問題。當用戶安裝應用程序,然後購買物品,然後卸載應用程序並重新安裝並轉到購買的項目,然後應用程序安捷倫鎖定被篡改的物品並要求購買請求。 OnRestoreTransaction中有沒有問題? 我使用Google In app Purchse網站的代碼。 下面是代碼:Google In-APP購買請求重新安裝APP

private class DungeonsPurchaseObserver extends PurchaseObserver { 
     public DungeonsPurchaseObserver(Handler handler) { 
      super(in_app.this, handler); 
     } 

     @Override 
     public void onBillingSupported(boolean supported) { 
      if (Consts.DEBUG) { 
       Log.i(TAG, "supported: " + supported); 
      } 
      if (supported) { 
       restoreDatabase(); 

      } else { 
       showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID); 
      } 
     } 

     @Override 
     public void onPurchaseStateChange(PurchaseState purchaseState, String itemId, 
       int quantity, long purchaseTime, String developerPayload) { 
      if (Consts.DEBUG) { 
       Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState); 
      } 

      if (purchaseState == PurchaseState.PURCHASED) { 
       finish(); 
      } 
     } 


     @Override 
     public void onRequestPurchaseResponse(RequestPurchase request, 
       ResponseCode responseCode) { 
      if (Consts.DEBUG) { 
       Log.d(TAG, request.mProductId + ": " + responseCode); 
      } 
      if (responseCode == ResponseCode.RESULT_OK) { 
       if (Consts.DEBUG) { 
        Log.i(TAG, "purchase was successfully sent to server"); 
       } 
      } else if (responseCode == ResponseCode.RESULT_USER_CANCELED) { 
       if (Consts.DEBUG) { 
        Log.i(TAG, "user canceled purchase"); 
       } 
      } else { 
       if (Consts.DEBUG) { 
        Log.i(TAG, "purchase failed"); 
       } 
      } 
     } 

     @Override 
     public void onRestoreTransactionsResponse(RestoreTransactions request, 
       ResponseCode responseCode) { 
      if (responseCode == ResponseCode.RESULT_OK) { 
       if (Consts.DEBUG) { 
        Log.d(TAG, "completed RestoreTransactions request"); 
       } 
       // Update the shared preferences so that we don't perform 
       // a RestoreTransactions again. 
       SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE); 
       SharedPreferences.Editor edit = prefs.edit(); 
       edit.putBoolean(DB_INITIALIZED, true); 
       edit.commit(); 
      } else { 
       if (Consts.DEBUG) { 
        Log.d(TAG, "RestoreTransactions error: " + responseCode); 
       } 
      } 
     } 
    } 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.in_app); 

     mHandler = new Handler(); 
     mDungeonsPurchaseObserver = new DungeonsPurchaseObserver(mHandler); 
     mBillingService = new BillingService(); 
     mBillingService.setContext(this); 

     mPurchaseDatabase = new PurchaseDatabase(this); 
     //setupWidgets(); 

     // Check if billing is supported. 
     ResponseHandler.register(mDungeonsPurchaseObserver); 
     if (!mBillingService.checkBillingSupported()) { 
      showDialog(DIALOG_CANNOT_CONNECT_ID); 
     } 



     purchase=(Button) findViewById(R.id.purchase); 
     cancel=(Button) findViewById(R.id.cancel); 

     purchase.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if(!ownedItems.contains("android.test.refunded")){  
        if (Consts.DEBUG) { 
         Log.d(TAG, "buying: " + " product" + " Product Name: " + "Product"); 
        } 

        if (!mBillingService.requestPurchase("android.test.refunded", mPayloadContents)) { 
         showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID); 
        } 
      } 
      } 
     }); 

編輯:

我稱這種現象onCrearte應用程序內的java文件: 這首先檢查whehter當前用戶購買的商品爲他的要求很高如果不是,則請求購買。 在onPurchsestateChanged我只檢查該狀態是否被購買。

public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.in_app);

mHandler = new Handler(); 
    mDungeonsPurchaseObserver = new DungeonsPurchaseObserver(mHandler); 
    mBillingService = new BillingService(); 
    mBillingService.setContext(this); 

    mPurchaseDatabase = new PurchaseDatabase(this); 
    //setupWidgets(); 

    // Check if billing is supported. 
    ResponseHandler.register(mDungeonsPurchaseObserver); 
    if (!mBillingService.checkBillingSupported()) { 
     showDialog(DIALOG_CANNOT_CONNECT_ID); 
    } 
    System.out.println("for check"+getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED, false)); 

    if (getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED, false)) { 
     System.out.println(mBillingService.restoreTransactions()); 
    } 
    else{ 
    purchase=(Button) findViewById(R.id.purchase); 
    cancel=(Button) findViewById(R.id.cancel); 

    purchase.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(!ownedItems.contains("android.test.refunded")){  
       if (Consts.DEBUG) { 
        Log.d(TAG, "buying: " + " product" + " Product Name: " + "i"); 
       } 


       if (!mBillingService.requestPurchase("android.test.refunded", mPayloadContents)) { 
        showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID); 
       } 
     } 
     } 
    }); 


    cancel.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      finish(); 
      startActivity(new Intent(in_app.this, Main.class)); 
      overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right); 
     } 
    }); 
    } 
} 

OnPurchaseStateChanged:

public void onPurchaseStateChange(PurchaseState purchaseState, String itemId, 
      int quantity, long purchaseTime, String developerPayload) { 

     if (Consts.DEBUG) { 
      Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState); 
     } 
     System.out.println("here for new fun"); 
     if (purchaseState == PurchaseState.PURCHASED) { 

      finish(); 
      Intent intent1 = new Intent(context,BodyParts1.class); 
      context.startActivity(intent1); 
     } 


    } 

回答

1

爲了恢復你必須顯式調用mBillingService.restoreTransactions()購買的所有物品。然後,您將收到每個以前購買的物品的onPurchaseStateChange回叫。

將這個代碼onCreate方法,它應該很好地工作:

if (!getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED, false)) { 
    mBillingService.restoreTransactions(); 
} 
+0

三江源的幫助..在這裏我要調用這個函數在我上面的代碼?意味着應用程序啓動此功能時應運行以檢查此特定用戶的購買項目。第二我在測試帳戶的應用程序測試,所以這個帳戶可以確定這個項目已被購買? – User42590

+0

檢查我的更新回答 –

+0

我在我的應用內文件中調用了這個。但是這個函數給出false並且它不執行mBillingService.restoreTransactions();我打印它檢查什麼值返回System.out.println(「for check」+ getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED,false));並且它的返回值是(false) – User42590