Service Intent must be explicit: Intent


I have an app some time now in which I call a service through a broadcast receiver (MyStartupIntentReceiver). The code in the broadcast receiver in order to call the service is:

public void onReceive(Context context, Intent intent) {
    Intent serviceIntent = new Intent();
    serviceIntent.setAction("com.duk3r.eortologio2.MyService");
    context.startService(serviceIntent);
}

The problem is that in Android 5.0 Lollipop I get the following error (in previous versions of Android, everything works ok):

Unable to start receiver com.duk3r.eortologio2.MyStartupIntentReceiver: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.duk3r.eortologio2.MyService }

What do I have to change in order for the service to be declared as explicit and start normally? Tried some answers in other similar threads but although i got rid of the message, the service wouldn't start.

share improve this question
 
 
is this a service in your app? –  tyczj  Jan 8 '15 at 14:28
 
yes the package com.duk3r.eortologio2 is my app. –  Tek  Jan 8 '15 at 14:29
3 
Only have an <intent-filter> on a component when you want third-party apps to communicate with that component. You seem to have fallen into the trap of assuming that you need an <intent-filter> for everything -- in reality, you infrequently need an <intent-filter>. An explicit Intent is when you designate the component to talk to in the Intent itself, usually using the constructor that takes a JavaClass object as the second parameter. That, rather than implicit Intents and <intent-filter>s, are what you should use for components local to your app. –  CommonsWare  Jan 8 '15 at 14:34 
 

4 Answers

up vote 21 down vote accepted

any intent you make to a service, activity etc. in your app should always follow this format

Intent serviceIntent = new Intent(context,MyService.class);
context.startService(serviceIntent);

or

Intent bi = new Intent("com.android.vending.billing.InAppBillingService.BIND");
bi.setPackage("com.android.vending");

implicit intents (what you have in your code currently) are considered a security risk

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值