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