您的位置:首页 > 大数据 > 人工智能

Service Intent must be explicit: Intent { act=com.example.evalee.aidl }

2017-04-06 15:33 465 查看
今天在复习Service和AIDL的时候发现一个以前一直在用的一种启动服务的方式,今天竟然不行了。

我原来的做法是现在AndroidManifest.xml中定义Service.

<service
android:name=".aidlService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.evalee.aidl"/>
</intent-filter>
</service>


因为是学习的AIDL,在客户端中绑定了Service,用的是之前习惯的做法。通过设置Action,启动服务。

Intent intent = new Intent();
intent.setAction("com.example.evalee.aidl");
bindService(intent,conn, Service.BIND_AUTO_CREATE);


结果就报错了。

/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.evalee.aidlclient, PID: 9530
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.evalee.aidlclient/com.example.evalee.aidlclient.AidlClient}:
java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.example.evalee.aidl }
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


原来在Android5.0之后,Service的Intent必须是要显性声明,,据说这是为了防止多个Service用同样的intent-filter的情况。

这里有两种方法可供参考。

1. 直接声明是那个服务类

new intent(MainActivity.this,MyService.class);


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("com.example.evalee.aidl");
final Intent eintent = new Intent(createExplicitFromImplicitIntent(this,intent));
bindService(eintent,conn, Service.BIND_AUTO_CREATE);


踏踏踏踏,完事。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: