2014-10-04 82 views
0

購買我在應用程序內購買高級功能,目前當你購買它,它會修改共享偏好,讓你的高級功能有一個。我想,如果你每次打開應用會從谷歌找出時間玩,如果用戶已經購買溢價,這樣,如果用戶切換設備時,他們將不必重新購買溢價。我認爲要做到這一點,我需要使用「查詢已購買的商品」這裏谷歌的教程的第二部分:http://developer.android.com/google/play/billing/billing_integrate.html問谷歌播放如果在應用程序內購買已被用戶

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); 
     } 
    }; 
    Intent serviceIntent = new Intent(
      "com.android.vending.billing.InAppBillingService.BIND"); 
    serviceIntent.setPackage("com.android.vending"); 
    bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE); 
    try { 
     Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null); 
    } catch (RemoteException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

代碼編譯沒有錯誤,如果我刪除行接收的所擁有的包項目的應用程序不會崩潰。但是,如果我嘗試實際收到應用程序將崩潰的信息。我在onCreate()方法如上所示的代碼和我有MSERVICE和mServiceConn在java文件的頂部引入。謝謝。

回答

0

bindService()是一個異步操作,這意味着你的MSERVICE幾乎可以肯定將是無效的調用bindService()之後。

你需要等待onServiceConnected()被稱爲和您查詢您購買前,收到有效mService - 無論是移動你的try塊到onServiceConnected()或者,在一個由onServiceConnected()調用的方法。

+0

由於我有你描述了使用異步操作不正確的問題。 – user3478306 2014-10-04 18:10:33

+0

如果這個幫助,請確保您接受的答案,從沒有答案的隊列中刪除。 – ianhanniballake 2014-10-04 18:17:34