title: Service Intent must be explicit的解決方案
date: 2017-09-14 15:11:43
tags:
- Android
categories: - Android開(kāi)發(fā)筆記
今天在學(xué)習(xí)AIDL的時(shí)候,通過(guò)以下步驟:
- 在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)過(guò)查找資料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è)比較麻煩的方法
先通過(guò)一個(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