1. 程式人生 > >Service Intent must be explicit的解決方案

Service Intent must be explicit的解決方案

今天在學習AIDL的時候,通過以下步驟:

  • 在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);

  • 執行程式,結果報錯了!
    java.lang.IllegalArgumentException: Service Intent must be explicit

這裡寫圖片描述
經過查詢資料Stackoverflow.com,發現是需要將隱含意圖轉換為顯示的意圖,然後啟動再服務。

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

1.先說一個簡單的辦法

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

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

2.另外一個比較麻煩的方法
先通過一個函式將隱式呼叫轉變為顯示呼叫

/*** 
     * 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;  
    }  

然後呼叫

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

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