如果用户已购买应用内购买,则询问Google Play

时间:2014-10-04 13:13:19

标签: android in-app-purchase google-play bundle

我在应用内购买高级功能,目前购买时,它会编辑共享首选项,为您提供高级功能。我希望如果你每次打开应用程序,它会从谷歌播放中找到,如果用户已购买溢价,以便如果用户切换设备,他们将不必重新购买溢价。我认为要做到这一点,我需要使用"查询购买的物品"谷歌教程的一部分: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();
    }

代码编译时没有错误,如果我删除该行以接收Bundle of owned items,则应用程序不会崩溃。但是,如果我尝试实际收到信息,应用程序将崩溃。我在onCreate()方法中有上面显示的代码,并且我在java文件的顶部引入了mService和mServiceConn。感谢。

1 个答案:

答案 0 :(得分:0)

bindService()是一个异步操作,这意味着在调用bindService()之后,你的mService几乎肯定会为空。

您需要等待onServiceConnected()被调用,并且在查询购买之前收到有效的mService - 将try块移至onServiceConnected()或将其移至onServiceConnected()在{{1}}调用的方法中。

相关问题