Service Intent must be explicit的解决方案

今天在学习AIDL的时候,通过以下步骤:

  • 在AndroidMenifest中声明service
1
2
3
4
5
6
7
8
9
10

<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
1
2
3
4
5

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
    20170914154711498

经过查找资料Stackoverflow.com,发现是需要将隐含意图转换为显示的意图,然后启动再服务。

所以综合Stackoverflow上给出的解决方案,现在大体上有两种解决的方法.

1.先说一个简单的办法

1
2
3
4
5
6
Intent mIntent = new Intent();
//自定义的Service的action
mIntent.setAction("XXX.XXX.XXX");
//自定义Service的包名
mIntent.setPackage(getPackageName());
context.startService(mIntent);

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

2.另外一个比较麻烦的方法
先通过一个函数将隐式调用转变为显示调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/***
* 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;
}

然后调用

1
2
3
4
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

Service Intent must be explicit: Intent

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
,