Android--launch service across apps

Source: Internet
Author: User

This chapter blog, which records the launch service across apps.
We need to create an application Service1, which contains a myservice, some of the code is as follows:

    @Override    publicvoidonCreate() {        // TODO Auto-generated method stub        System.out.println("onCreate");        super.onCreate();    }    @Override    publicvoidonDestroy() {        System.out.println("onDestroy");        super.onDestroy();    }

Previous blogs have introduced the Create service, which can be referenced in Create: http://blog.csdn.net/gaopeng0071/article/details/45153495

Then create a Service2 app. The activity code is as follows,
We note 21 lines of code, the first parameter in the ComponentName object is the service package name to jump to the app, and the second parameter is the full path of the service class.

 PackageCom.example.service2;Importandroid.app.Activity;ImportAndroid.content.ComponentName;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener; Public  class mainactivity extends Activity implements Onclicklistener  {Intent serviceintent;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Serviceintent =NewIntent (); Serviceintent.setcomponent (NewComponentName ("Com.example.service1","Com.example.service1.MyService")); Findviewbyid (R.id.button1). Setonclicklistener ( This); Findviewbyid (R.id.button2). Setonclicklistener ( This); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true; }@Override     Public void OnClick(View v) {Switch(V.getid ()) { CaseR.id.button1:startservice (serviceintent); Break; CaseR.id.button2:stopservice (serviceintent); Break; }    }}

Jump through 21 lines of code to enable service launch across apps.

Android--launch service across apps

Related Article

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.