duk*_*k3r 66 service android broadcastreceiver
我现在有一个应用程序,我通过广播接收器(MyStartupIntentReceiver)调用服务.广播接收器中用于呼叫服务的代码是:
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.duk3r.eortologio2.MyService");
context.startService(serviceIntent);
}
Run Code Online (Sandbox Code Playgroud)
问题是在Android 5.0 Lollipop中我收到以下错误(在以前版本的Android中,一切正常):
Unable to start receiver com.duk3r.eortologio2.MyStartupIntentReceiver: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.duk3r.eortologio2.MyService }
Run Code Online (Sandbox Code Playgroud)
为了将服务声明为显式并正常启动,我需要更改什么?尝试在其他类似的线程中的一些答案,但虽然我摆脱了消息,服务将无法启动.
tyc*_*czj 111
您对应用中的服务,活动等所做的任何意图都应始终遵循此格式
Intent serviceIntent = new Intent(context,MyService.class);
context.startService(serviceIntent);
Run Code Online (Sandbox Code Playgroud)
要么
Intent bi = new Intent("com.android.vending.billing.InAppBillingService.BIND");
bi.setPackage("com.android.vending");
Run Code Online (Sandbox Code Playgroud)
隐式意图(当前代码中包含的内容)被视为安全风险
小智 29
设置你的packageName
作品.
intent.setPackage(this.getPackageName());
Run Code Online (Sandbox Code Playgroud)
将隐式意图转换为显式意图,然后启动服务。
Intent implicitIntent = new Intent();
implicitIntent.setAction("com.duk3r.eortologio2.MyService");
Context context = getApplicationContext();
Intent explicitIntent = convertImplicitIntentToExplicitIntent(implicitIntent, context);
if(explicitIntent != null){
context.startService(explicitIntent);
}
public static Intent convertImplicitIntentToExplicitIntent(Intent implicitIntent, Context context) {
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfoList = pm.queryIntentServices(implicitIntent, 0);
if (resolveInfoList == null || resolveInfoList.size() != 1) {
return null;
}
ResolveInfo serviceInfo = resolveInfoList.get(0);
ComponentName component = new ComponentName(serviceInfo.serviceInfo.packageName, serviceInfo.serviceInfo.name);
Intent explicitIntent = new Intent(implicitIntent);
explicitIntent.setComponent(component);
return explicitIntent;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
59070 次 |
最近记录: |