First, use service 1. Right-click the Java folder, select New Service, and then override the Onstartcommand function, as long as the StartService function is executed, Onstartcommand will be executed
Package com.example.shiyanshi.learnservice;
Import Android.app.Service;
Import android.content.Intent;
Import Android.os.Binder;
Import Android.os.IBinder;
public class MyService extends Service {
Public MyService () {
}
@Override
Public IBinder Onbind (Intent Intent) {
Todo:return the communication channel to the service.
throw new Unsupportedoperationexception ("not yet implemented");
return new Binder (); To be rewritten, the above error will pop up
}
Called when executing StartService
@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
System.out.println ("Service onstartcommand");
New Thread (New Runnable () {
@Override
public void Run () {
while (true) {
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println ("******************service is running********************");
}
}
}). Start ();
Return Super.onstartcommand (Intent, flags, Startid);
}
@Override
Public void OnCreate () {
super.oncreate ();
System.out.println ("**************service oncreate*****************");
// }
//
@Override
Public void OnDestroy () {
Super.ondestroy ();
System.out.println ("****************service ondestroy***************");
// }
}
2. Call the StartService function when the service is started, call the StopService function when the service is closed, the function requires a intent, whether to create intent as follows or StartService (new Intent (Mainactivity.this,myservice.class)); StopService (new Intent (Mainactivity.this,myservice.class)); Services that are turned on or off are the same
public class Mainactivity extends Activity implements View.onclicklistener, Serviceconnection {
Private Intent Intent;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Intent=new Intent (Mainactivity.this,myservice.class);
Findviewbyid (R.id.btnstartservice). Setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (view view) {
StartService (Intent);
}
});
Findviewbyid (R.id.btnstopservice). Setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (view view) {
StopService (Intent);
}
});
Second, binding Service1. Call Bindservice function when binding service, public abstract Boolean bindservice (Intent service, Serviceconnection Conn, int flags) Its second parameter represents the connection of the service, which is used primarily to listen for the state of the service and can pass the this pointer (the service is bound to the current activity when this is passed, and when exiting the current activity, The service will automatically be shut down by the system (call the OnDestroy function and an error message will pop up), and the third parameter can be consideredBIND_AUTO_CREATE,BIND_DEBUG_UNBIND,BIND_NOT_FOREGROUND,BIND_ABOVE_CLIENT,BIND_ALLOW_OOM_MANAGEMENT, orBIND_WAIVE_PRIORITYNote that when the second argument passes this, onserviceconnected and onservicedisconnected are also implemented in the current Activity class, otherwise there will be a type mismatch error. Where the onserviceconnected function executes after the binding service succeeds, the process in which the Onservicedisconnected service is located crashes or is killed.
public class Mainactivity extends Activity implements View.onclicklistener, Serviceconnection {
Private Intent Intent;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Intent=new Intent (Mainactivity.this,myservice.class);
Findviewbyid (R.id.btnstartservice). Setonclicklistener (this);
Findviewbyid (R.id.btnstopservice). Setonclicklistener (this);
Findviewbyid (R.id.btnbindservice). Setonclicklistener (this);
Findviewbyid (R.id.btnunbindservice). Setonclicklistener (this);
}
@Override
public void OnClick (view view) {
Switch (View.getid ())
{
Case R.id.btnstartservice:
StartService (Intent);
Break
Case R.id.btnstopservice:
StopService (Intent);
Break
Case R.id.btnbindservice:
Bindservice (Intent, this, context.bind_auto_create);
Break
Case R.id.btnunbindservice:
Unbindservice (this);
Break
Default
Break
}
}
Service is successfully executed after successful binding
@Override
public void onserviceconnected (ComponentName componentname, IBinder ibinder) {
System.out.println ("***************service Connect successful********************");
}
The process in which the service is located is executed when it crashes or is killed
@Override
public void onservicedisconnected (ComponentName componentname) {
SYSTEM.OUT.PRINTLN ("***************service disconnect successful********************");
}
}
public class MyService extends Service {
Public MyService () {
}
@Override
Public IBinder Onbind (Intent Intent) {
Todo:return the communication channel to the service.
throw new Unsupportedoperationexception ("not yet implemented");
return new Binder ();
}
Called when executing StartService
@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
System.out.println ("Service onstartcommand");
Return Super.onstartcommand (Intent, flags, Startid);
}
@Override
public void OnCreate () {
Super.oncreate ();
System.out.println ("**************service oncreate*****************");
}
@Override
public void OnDestroy () {
Super.ondestroy ();
System.out.println ("****************service ondestroy***************");
}
}
2.Service Program
public class MyService extends Service {
Public myservice () {
}
@Override
Public Ibin Der Onbind (Intent Intent) {
//Todo:return The communication channel to the service.
throw new Unsupportedoperationexception ("not yet implemented");
return new Binder ();
}
//called when executing startservice
@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
System.out.println ("Service onstartcommand");
Return Super.onstartcommand (Intent, flags, Startid);
}
@Override
public void OnCreate () {
super.oncreate ();
System.out.println ("**************service oncreate*****************");
}
@Override
public void OnDestroy () {
Super.ondestroy ();
System.out.println ("****************service ondestroy***************");
}
}
Three Service life cycle 1.onCreate start or bind service execution, note that if the service is started, and the service is bound, the function will only be executed once (the first start or binding of the executed) 2.onDestroy unbind or stop the service is executed, note that if the service is started at the same time, binding Works The function will only be executed when the service is stopped and the binding is lifted (the execution of the last stop service or unbind) 3.onStartCommand can be executed multiple times, as long as the StartService function is executed, the function will be executed; Note that the OnCreate function is only executed for the first time Startservic The E function (or Bindservice function) is executed only once. 4.onServiceConnected only executes after the service binding succeeds, note that it can only be performed once. The process of 5.onServiceDisconnected service is executed when it crashes or is killed.
(v) Recognize the service in Android