Android Development via intent launch service for other apps

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.