谷歌IAB助手bindService崩溃

时间:2016-08-20 11:46:14

标签: android google-play in-app-billing android-billing

我遇到以下异常:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{myapppackage2/myapppackage.ui.MainActivity}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.android.vending.billing.InAppBillingService.BIND }
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
       at android.app.ActivityThread.access$800(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
       at android.os.Handler.dispatchMessage(Handler.java)
       at android.os.Looper.loop(Looper.java)
       at android.app.ActivityThread.main(ActivityThread.java)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
Caused by java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.android.vending.billing.InAppBillingService.BIND }
       at android.app.ContextImpl.validateServiceIntent(ContextImpl.java)
       at android.app.ContextImpl.bindServiceCommon(ContextImpl.java)
       at android.app.ContextImpl.bindService(ContextImpl.java)
       at android.content.ContextWrapper.bindService(ContextWrapper.java)
       at com.android.vending.billing.util.IabHelper.doBindService(IabHelper.java:277)
       at com.android.vending.billing.util.IabHelper.startSetup(IabHelper.java:268)
       at myapppackage.feature.FeaturesManager.initBilling(FeaturesManager.java:79)
       at myapppackage.ui.MainActivity.onCreate(MainActivity.java:374)
       at android.app.Activity.performCreate(Activity.java)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
       at android.app.ActivityThread.access$800(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
       at android.os.Handler.dispatchMessage(Handler.java)
       at android.os.Looper.loop(Looper.java)
       at android.app.ActivityThread.main(ActivityThread.java)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)

我用Google搜索了一下,发现我应该添加serviceIntent.setPackage("com.android.vending");,但问题是我已经拥有它了:

Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
        serviceIntent.setPackage("com.android.vending");
        List<ResolveInfo> resolveInfos = mContext.getPackageManager().queryIntentServices(serviceIntent, 0);
        if (resolveInfos != null && !resolveInfos.isEmpty()) {
            // service available to handle that Intent
            mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
        }
        else {
            // no service available to handle that Intent
            if (listener != null) {
                listener.onIabSetupFinished(
                    new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                        "Billing service unavailable on device."));
            }
        }

xiaomi mi-4c,android 5.1.1,rooted。

有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

首先使用

在您的设备中检查Google服务
public static boolean checkPlayServices(Context mContext) {
    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int resultCode = api.isGooglePlayServicesAvailable(mContext);
    return resultCode == ConnectionResult.SUCCESS;
}

然后使用它。

try {
    Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");
    mContext.bindService(serviceIntent, SceneSelection.this, Context.BIND_AUTO_CREATE);
    } catch (RuntimeException ignored) {
       if (listener != null) {
          listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                                "Billing service unavailable on device."));
            }
 }