1. Start a service after Android app is opened
public class Testserviceactivity extends Activity {
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
System.out.println ("Activity Run Thread ID:" + thread.currentthread (). GetName () + "--" +
Thread.CurrentThread (). GetId ());
System.out.println ("Activity's Process ID:" +android.os.process.mypid ());
Intent Intent = new Intent (this,service1.class);
StartService (Intent);
}
}
2. First service
public class Service1 extends Service {
@Override
Public IBinder Onbind (Intent Intent) {
TODO auto-generated Method Stub
return null;
}
@Override
public void OnCreate () {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("Service 1 is turned on");
SYSTEM.OUT.PRINTLN ("Service Run thread ID:" + thread.currentthread (). GetName () + "--" +
Thread.CurrentThread (). GetId ());
SYSTEM.OUT.PRINTLN ("Process ID of the service:" +android.os.process.mypid ());
Super.oncreate ();
}
@Override
public void OnDestroy () {
Intent Intent = new Intent (this,service2.class);
StartService (Intent);
Super.ondestroy ();
}
}
3.2nd Service
public class Service2 extends Service {
@Override
Public IBinder Onbind (Intent Intent) {
TODO auto-generated Method Stub
return null;
}
@Override
public void OnCreate () {
SYSTEM.OUT.PRINTLN ("Service 2 is turned on");
Super.oncreate ();
}
@Override
public void OnDestroy () {
Intent Intent = new Intent (this,service1.class);
StartService (Intent);
Super.ondestroy ();
}
}
4. In the manifest file
<service android:name= ". Service1 "
Android:process= "CN.IT.YQQ"
></service>
<service android:name= ". Service2 "></service>
Android app multi-threaded guardian makes it hard to kill it