An example of the implementation of the function is:
1, after the installation program to look at an activity program interface, there is a button, click the button will start a service, at this time in the Setup program management will see an activity and a service services run
2, if the phone shutdown restart, will trigger the service inside your program, of course, the phone after the boot is not visible to your program interface. Like the alarm clock function inside the phone, the phone restart can not see the Alarm settings screen
Just start the service, the time is up, the alarm clock is good ringing reminder.
The program code is:
First of all, there is a start-up activity for the boot, to your button set Onclicklistener ();
public class Mainactivity extends Activity {private Button btnstarted = null; public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main); btnstarted = (Button) Findviewbyid (r.id.btnstarted); Btnstarted.setonclicklistener (New Onclicklistener () {public void OnClick (View v) { Intent Intent = new Intent ( Mainactivity.this,startservice.class); StartService (intent); Toast.maketext (Mainactivity.this, "service started successfully", Toast.length_long). Show ();}} ;}}
We're going to write a broadcastreceiver to capture the action_boot_completed broadcast and start the service we want to start after capturing Starservie.class
public class Bootcompletedreceiver extends broadcastreceiver{public void OnReceive (context context, Intent Intent) { if (intent.getaction (). Equals (intent.action_boot_completed)) { Intent newintent = new Intent (context, Startservice.class); Context.startservice (newintent);}}}
Start Service Code
public class StartService extends service{//public static String Phoneno, public class Localbinder extends binder{ St Artthief GetService () { return startservice.this;} public IBinder onbind (Intent Intent) { return mbinder;} private void Registerintentreceiver () { ///Here Add operation code to be executed by the start service } public void OnStart (Intent intent,int startid) { Super.onstart (Intent, Startid);} @Override public void OnCreate () { registerintentreceiver (); }}
With the Main.xml, there is only one button, the ID is btnstarted
<?XML version= "1.0" encoding= "Utf-8"?><AbsolutelayoutAndroid:id= "@+id/absolutelayout01"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"xmlns:android= "Http://schemas.android.com/apk/res/android"> <ButtonAndroid:layout_height= "Wrap_content"Android:id= "@+id/btnstarted"Android:text= "@string/started"android:layout_y= "118dip"Android:layout_width= "Wrap_content"android:layout_x= "56dip"> </Button></Absolutelayout>
Registering our Broadcastreceiver and service services in the Androidmanifest.xml configuration file
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.thief"Android:versioncode= "1"Android:versionname= "1.0"> <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name"> <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity>//Registration Services<ServiceAndroid:name=". StartService "></Service>//In order to get boot start this action, must register plus Android.intent.action.BOOT_COMPLETED<receiverAndroid:name=". Bootcompletedreceiver "> <Intent-filter> <ActionAndroid:name= "Android.intent.action.BOOT_COMPLETED"> </Action> </Intent-filter> </receiver> </Application>get permission to start the boot action permission<uses-permissionAndroid:name= "Android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>" </Manifest>
Android Development Tutorial Boot Service Example