1 PackageCOM.EXAMPLE.MYAPP4;2 3 Importandroid.support.v7.app.ActionBarActivity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.Menu;7 ImportAndroid.view.MenuItem;8 ImportAndroid.view.View;9 ImportAndroid.widget.Button;Ten /** One * Android Record service life cycle (need to introduce service class name in configuration file) A * @authorshaobn - * - */ the Public classMainactivityextendsactionbaractivity { - PrivateButton Startbutton; - PrivateButton Stopbutton; - @Override + protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); AStartbutton = (Button) This. Findviewbyid (r.id.button1); atStopbutton = (Button) This. Findviewbyid (r.id.button2); -Startbutton.setonclicklistener (NewMyclick ()); -Stopbutton.setonclicklistener (NewMyclick ()); - } - Public classMyclickImplementsview.onclicklistener{ - @Override in Public voidOnClick (View arg0) { - //TODO auto-generated Method Stub to Switch(Arg0.getid ()) { + CaseR.id.button1: -Intent Intent =NewIntent (mainactivity. This, MyService.class); the StartService (intent); * Break; $ Panax Notoginseng CaseR.id.button2: -Intent Intent2 =NewIntent (mainactivity. This, MyService.class); the StopService (intent2); + Break; A } the } + - } $}
1 PackageCOM.EXAMPLE.MYAPP4;2 3 ImportAndroid.app.Service;4 Importandroid.content.Intent;5 ImportAndroid.os.IBinder;6 7 Public classMyServiceextendsService {8 @Override9 Public voidonCreate () {Ten //TODO auto-generated Method Stub OneSystem.out.println ("--create"); A Super. OnCreate (); - } - @Override the Public intOnstartcommand (Intent Intent,intFlagsintStartid) { - //TODO auto-generated Method Stub -System.out.println ("--onstartcommand"); - return Super. Onstartcommand (Intent, flags, startid); + } - @Override + Publicibinder onbind (Intent arg0) { A //TODO auto-generated Method Stub at return NULL; - } - @Override - Public voidOnDestroy () { - //TODO auto-generated Method Stub -System.out.println ("--ondestroy"); in Super. OnDestroy (); - } to +}
Life cycle of Android test service