Tarun Deep Attri
Tarun Deep Attri

Reputation: 8674

Why Service Intent Needs to be Explicit Intent

When we start a service like following :

Intent in = new Intent();
in.setAction("com.android.myAction");
startService(in);

It gives an error : Service Intent must be explicit.

Why is this so. Why android requires service intent to be explicit?

Upvotes: 6

Views: 806

Answers (3)

Tarun Deep Attri
Tarun Deep Attri

Reputation: 8674

A little more to this : If we want to use Implicit Intent(With Action Name), we have to use setPackageName like this :

Intent intent = new Intent();
intent.setPackage("com.action.ServicePackageName");
intent.setAction("com.action.ActionNameOfService");
bindService(intent, yourServiceConectionObject, Service.BIND_AUTO_CREATE);
//or
//startService(intent);

Point to note however is that if Service is local Service; You should use Explicit intent as Service Class is available to you. We can also use in same way as shown above but using Explicit is recommended for same reasons mentioned in some of answers. For Remote Services(Services in Other apps); Since we can not have Service class available to us; We have to bind to service this way only.

Upvotes: 1

dn_c
dn_c

Reputation: 614

When you start a service with implicit intent unlike Activity, no user interface is involved. When multiple Services can handle an Intent, Android selects one at random; the user is not prompted to select a Service.

In case the malicious Service is bound to the calling application, then the attacker can return arbitrary malicious data or simply return a successful result without taking the requested action. The malicious Service can steal data and lie about completing requested actions.

Upvotes: 2

Vitalii Obideiko
Vitalii Obideiko

Reputation: 1943

"To ensure your app is secure, always use an explicit intent when starting or binding your Service and do not declare intent filters for the service."(From Android developer) It must be because without it other apps will can start your service and etc.

Upvotes: 1

Related Questions