Service Intent must be explicit的解決方案


title: Service Intent must be explicit的解決方案
date: 2017-09-14 15:11:43
tags:

  • Android
    categories:
  • Android開發(fā)筆記

今天在學(xué)習(xí)AIDL的時(shí)候,通過以下步驟:

  • 在AndroidMenifest中聲明service

    <service
        android:name=".MyService"
        android:process=":remote"
        android:exported="true">
        <intent-filter >
            <category android:name="android.intent.category.DEFAULT"></category>
            <action android:name="com.ihealth.learnaidl.MyService"></action>
        </intent-filter>
    </service>

  • 在客戶端中綁定service

Intent intentService=new Intent();
                intentService.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intentService.setAction(ACTION_BIND_SERVICE);
                MainActivity.this.bindService(intentService,mServiceConnection,BIND_AUTO_CREATE);


  • 運(yùn)行程序,結(jié)果報(bào)錯(cuò)了!
    java.lang.IllegalArgumentException: Service Intent must be explicit
    20170914154711498

經(jīng)過查找資料Stackoverflow.com,發(fā)現(xiàn)是需要將隱含意圖轉(zhuǎn)換為顯示的意圖,然后啟動(dòng)再服務(wù)。

所以綜合Stackoverflow上給出的解決方案,現(xiàn)在大體上有兩種解決的方法.

1.先說(shuō)一個(gè)簡(jiǎn)單的辦法

Intent mIntent = new Intent();
//自定義的Service的action
mIntent.setAction("XXX.XXX.XXX");
//自定義Service的包名
mIntent.setPackage(getPackageName());
context.startService(mIntent);

即只需要多加一句話mIntent.setPackage(getPackageName());就可以解決.

2.另外一個(gè)比較麻煩的方法
先通過一個(gè)函數(shù)將隱式調(diào)用轉(zhuǎn)變?yōu)轱@示調(diào)用

/***
     * Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent,
     * "java.lang.IllegalArgumentException: Service Intent must be explicit"
     *
     * If you are using an implicit intent, and know only 1 target would answer this intent,
     * This method will help you turn the implicit intent into the explicit form.
     *
     * Inspired from SO answer: http://stackoverflow.com/a/26318757/1446466
     * @param context
     * @param implicitIntent - The original implicit intent
     * @return Explicit Intent created from the implicit original intent
     */
    public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
        // Retrieve all services that can match the given intent
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);

        // Make sure only one match was found
        if (resolveInfo == null || resolveInfo.size() != 1) {
            return null;
        }

        // Get component info and create ComponentName
        ResolveInfo serviceInfo = resolveInfo.get(0);
        String packageName = serviceInfo.serviceInfo.packageName;
        String className = serviceInfo.serviceInfo.name;
        ComponentName component = new ComponentName(packageName, className);

        // Create a new intent. Use the old one for extras and such reuse
        Intent explicitIntent = new Intent(implicitIntent);

        // Set the component to be explicit
        explicitIntent.setComponent(component);

        return explicitIntent;
    }

然后調(diào)用

Intent intent = new Intent();
                intent.setAction(ACTION_BIND_SERVICE);
                final Intent eintent = new Intent(createExplicitFromImplicitIntent(this,intent));
                bindService(eintent,mServiceConnection, Service.BIND_AUTO_CREATE);

兩種辦法都可以解決.記下來(lái),免得以后忘了.
感謝Stackoverflow~Google In-App billing, IllegalArgumentException: Service Intent must be explicit, after upgrading to Android L Dev Preview

Service Intent must be explicit: Intent

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容