Before Android5.0 can start the service of other app through the implicit intent way, just like the activity initiates an implicit intent.
However, after 5.0, it can only be started using the intent mode shown.
The intent SetComponent () method is required to launch the service for other apps. This method needs to pass in the ComponentName component parameter.
explanation of the parameters: component, The name of the application component to handle the intent, or null to let the system find one for you.
Code:
1 Intent serviceintent;2 3 PrivateButton Btnstartservice;4 PrivateButton Btnstopservice;5 6 @Override7 protected voidonCreate (Bundle savedinstancestate) {8 Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_main);Ten OneServiceintent=NewIntent (); AServiceintent.setcomponent (NewComponentName ("Com.example.startservicefromanotherapp", "Com.example.startservicefromanotherapp.AppService")); - -Btnstartservice=(Button) Findviewbyid (r.id.btnstartservice); theBtnstopservice=(Button) Findviewbyid (R.id.btnstopservice); - -Btnstartservice.setonclicklistener ( This); -Btnstopservice.setonclicklistener ( This); + } - + @Override A Public voidOnClick (View v) { at //TODO auto-generated Method Stub - Switch(V.getid ()) { - CaseR.id.btnstartservice: - StartService (serviceintent); - Break; - in CaseR.id.btnstopservice: - StopService (serviceintent); to Break; + } -}
This will enable the service of other apps. But you need to set up the service for the other app first.
1 android:exported= "true"
Otherwise the error will be Java.lang.SecurityException:Not allowed to start service Intent {cmp=com.example.startservicefromanotherapp/. Appservice} without permission not exported from UID 10075
Prompt does not have permission.
Android Development via intent launch service for other apps