Google 应用内计费,IllegalArgumentException:升级到 Android L Dev Preview 后必须明确服务意图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24480069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:16:23  来源:igfitidea点击:

Google In-App billing, IllegalArgumentException: Service Intent must be explicit, after upgrading to Android L Dev Preview

androidandroid-intentin-app-billingillegalargumentexceptionandroid-5.0-lollipop

提问by elliptic1

My in-app billing code was working fine until I upgraded to the Android L Dev Preview. Now I get this error when my app starts. Does anyone know what's changed about L that causes this or how I should change my code to fix this?

在我升级到 Android L Dev Preview 之前,我的应用内计费代码运行良好。现在,当我的应用程序启动时出现此错误。有谁知道 L 发生了什么变化导致了这个问题,或者我应该如何更改我的代码来解决这个问题?

android {
compileSdkVersion 'android-L'
buildToolsVersion '20'
defaultConfig {
    minSdkVersion 13
    targetSdkVersion 'L'
...
...


compile 'com.google.android.gms:play-services:5.+'
compile 'com.android.support:support-v13:21.+'
compile 'com.android.support:appcompat-v7:21.+'
...
...

The error when the app starts:

应用程序启动时的错误:

06-29 16:22:33.281    5719-5719/com.tbse.wnswfree D/AndroidRuntime﹕ Shutting down VM
06-29 16:22:33.284    5719-5719/com.tbse.wnswfree E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.tbse.wnswfree, PID: 5719
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tbse.wnswfree/com.tbse.wnswfree.InfoPanel}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.android.vending.billing.InAppBillingService.BIND }
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
        at android.app.ActivityThread.access0(ActivityThread.java:143)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5070)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
 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:1603)
        at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1702)
        at android.app.ContextImpl.bindService(ContextImpl.java:1680)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:528)
        at com.tbse.wnswfree.util.IabHelper.startSetup(IabHelper.java:262)
        at com.tbse.wnswfree.InfoPanel.onStart(InfoPanel.java:709)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1217)
        at android.app.Activity.performStart( Activity.java:5736)
        at android.app.ActivityThread.performLaunchActivity( ActivityThread.java:2218)
        at android.app.ActivityThread.handleLaunchActivity( ActivityThread.java:2317)
        at android.app.ActivityThread.access0( ActivityThread.java:143)
        at android.app.ActivityThread$H.handleMessage( ActivityThread.java:1258)
        ...

???????????

????????????

Line 709 in InfoPanel.java:

InfoPanel.java 中的第 709 行:

        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
          @Override
          public void onIabSetupFinished(IabResult result) {
            ...

回答by alav

I had the same problem and explicitly setting the package solved it. Similar to Aleksey's answer, but simpler:

我有同样的问题,明确设置包解决了它。类似于 Aleksey 的答案,但更简单:

Intent intent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
// This is the key line that fixed everything for me
intent.setPackage("com.android.vending");

getContext().bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

回答by Aleksey Masny

As pointed out in answer below, solutions would be to create explicit intent manually:

正如下面的回答所指出的,解决方案是手动创建明确的意图:

private Intent getExplicitIapIntent() {
        PackageManager pm = mContext.getPackageManager();
        Intent implicitIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
        List<ResolveInfo> resolveInfos = pm.queryIntentServices(implicitIntent, 0);

        // Is somebody else trying to intercept our IAP call?
        if (resolveInfos == null || resolveInfos.size() != 1) {
            return null;
        }

        ResolveInfo serviceInfo = resolveInfos.get(0);
        String packageName = serviceInfo.serviceInfo.packageName;
        String className = serviceInfo.serviceInfo.name;
        ComponentName component = new ComponentName(packageName, className);
        Intent iapIntent = new Intent();
        iapIntent.setComponent(component);
        return iapIntent;
    }

Hereis the code in L preview sources for check explicit intent. It's got commentedcurrently, but on Nexus 5 with L preview it still runs and throws exception for implicit intents.

是 L 预览源中用于检查显式意图的代码。它目前得到了评论,但在带有 L 预览的 Nexus 5 上它仍然运行并为隐式意图抛出异常。


Edit: @alav's answeris much more better and simpler. Just add


编辑:@alav 的答案更好更简单。只需添加

intent.setPackage("com.android.vending");

All credits for him. And hereis the code in L release sources for check explicit intent.

他的所有功劳。而这里为L排放源代码进行检查明确意图。

回答by Roy

Found clear solution here: https://code.google.com/p/android-developer-preview/issues/detail?id=1674

在这里找到了明确的解决方案:https: //code.google.com/p/android-developer-preview/issues/detail?id=1674

In Google Licensing Library (LVL), LicenseChecker.java file, replace "bindService" call with this:

在 Google 许可库 (LVL) 的 LicenseChecker.java 文件中,将“bindService”调用替换为:

 Intent serviceIntent = new Intent(
         new String(Base64.decode("Y29tLmFuZHJvaWQudmVuZGluZy5saWNlbnNpbmcuSUxpY2Vuc2luZ1NlcnZpY2U=")));
         serviceIntent.setPackage("com.android.vending");

     boolean bindResult = mContext
             .bindService(
               serviceIntent,
               this, // ServiceConnection.
               Context.BIND_AUTO_CREATE);

AND in the AndroidManifest.xml set: android:minSdkVersion="4"

并且在 AndroidManifest.xml 集合中:android:minSdkVersion="4"

The "setPackage" requires Android version 4.

“setPackage”需要 Android 版本 4。

回答by cketti

In "L" binding to a service requires using an explicit intent.

在“L”绑定到服务需要使用明确的意图。

See http://commonsware.com/blog/2014/06/29/dealing-deprecations-bindservice.html

http://commonsware.com/blog/2014/06/29/dealing-deprecations-bindservice.html

回答by Harpreet

Just replace the code

只需更换代码

boolean attempt = mContext.bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"),
                mServiceConn, Context.BIND_AUTO_CREATE);

with the following code

使用以下代码

Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
        serviceIntent.setPackage("com.android.vending");
        boolean attempt = mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);

in the class IabHelperwhich you have placed in the inappbilling's utils folder (if you have followed the instruction of Google InApp Billing tutorial).

IabHelper您放置在 inappbilling 的 utils 文件夹中的类中(如果您遵循了 Google InApp Billing 教程的说明)。

回答by Don Park

I was getting the same error from older Google Cloud Messaging setup code. The simplest fix appears to be changing

我从旧的 Google Cloud Messaging 设置代码中得到了同样的错误。最简单的修复似乎正在改变

Intent registrationIntent = new Intent(
        "com.google.android.c2dm.intent.REGISTER");

into

进入

Intent registrationIntent = new Intent();
registrationIntent.setClassName("com.google.android.c2dm.intent", "REGISTER");

回答by Mike Ludwig

This worked for me but I'd like to know of it's an acceptable way to do this:

这对我有用,但我想知道这是一种可以接受的方法:

i.setClass(context, MyService.class);

i.setClass(context, MyService.class);

回答by Bj?rn Kechel

For me it worked to use the current IabHelper from the samples: sdk/extras/google/play_billing/samples/TrivialDrive/src/com/example/android/trivialdrivesample/util/IabHelper.java

对我来说,它可以使用示例中的当前 IabHelper:sdk/extras/google/play_billing/samples/TrivialDrive/src/com/example/android/trivialdrivesample/util/IabHelper.java

Don't forget to run the sdk update manager first to make sure that you have the current version installed.

不要忘记先运行 sdk 更新管理器以确保您安装了当前版本。

回答by zaifrun

The answers for this specific problems have already been posted, but just to help out others with the exact same problem, but this time for the Licence API.

这个特定问题的答案已经发布,但只是为了帮助其他人解决完全相同的问题,但这次是针对许可证 API。

You get the same error on 5.0 message as in the IAP library posted above, but you can find a fix (involving manually changing a few lines in LicenseChecker.java (Google's code) and then recompiling your project that will include this library).

您在 5.0 消息中遇到与上面发布的 IAP 库中相同的错误,但您可以找到修复程序(涉及手动更改 LicenseChecker.java(Google 的代码)中的几行,然后重新编译将包含此库的项目)。

Check out: https://code.google.com/p/android/issues/detail?id=78505for details. Hope anyone can use it.

查看:https: //code.google.com/p/android/issues/detail?id=78505了解详情。希望任何人都可以使用它。

回答by ethemsulan

if you have below error please set targetSdkVersion 19 in the build.gradle. When i set 19 my problem solved. For publish i set targetSdkVersion 27

如果您有以下错误,请在 build.gradle 中设置 targetSdkVersion 19。当我设置 19 时,我的问题解决了。对于发布,我设置了 targetSdkVersion 27

at com.google.android.vending.licensing.LicenseChecker.checkAccess(LicenseChecker.java:150) at com.google.android.vending.expansion.downloader.impl.DownloaderService$LVLRunnable.run

在 com.google.android.vending.licensing.LicenseChecker.checkAccess(LicenseChecker.java:150) 在 com.google.android.vending.expansion.downloader.impl.DownloaderService$LVLRunnable.run

defaultConfig {
    applicationId "com.brain.math.game.free"
    minSdkVersion 15
    targetSdkVersion 19 

targetSdkVersion 19

目标SDK版本19