Sample startup service service for android development tutorial

  • 2020-05-27 07:12:52
  • OfStack

The functions of this example are:
1. After installing the program, you can see an Activity program interface, in which there is a button, click which will launch an Service service. At this time, you can see an Activity and an Service service running in the setup program management
2. If the phone is turned off and restarted, it will trigger the Service service in your application. Of course, your application interface cannot be seen after the phone is started. Like the built-in alarm function of the phone, you can't see the alarm setting interface when the phone restarts
Just start the service, and when it's time, the alarm will ring.

The program code is:

First of all, there should be an Activity for starting up and setting OnClickListener() for your button;

public class MainActivity extends Activity {
 private Button btnstarted = null;
    public void onCreate(Bundle savedInstanceState) {
     btnstarted = (Button)findViewById(;
     btnstarted.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this,StartService.class);
    Toast.makeText(MainActivity.this, " Service started successfully ", Toast.LENGTH_LONG).show();

We'll write an BroadcastReceiver to capture the ACTION_BOOT_COMPLETED broadcast, and after that we'll start the service we want to start, StarServie.class

public class BootCompletedReceiver extends BroadcastReceiver{ 
 public void onReceive(Context context, Intent intent) {
   Intent newIntent = new Intent(context,StartService.class);

Start the service Service code

public class StartService extends Service{
 //public static String PHONENO;

 public class LocalBinder extends Binder{
  StartThief getService(){
   return StartService.this;
 public IBinder onBind(Intent intent){
  return mBinder;
 private void registerIntentReceiver(){
  // Add the action code to start the service here  
 public void onStart(Intent intent,int startId){
  super.onStart(intent, startId);
    public void onCreate() {

Main.xml, there's only one Button,id is btnstarted

<?xml version="1.0" encoding="utf-8"?>
 <Button android:layout_height="wrap_content" 

Register our BroadcastReceiver and service Service in the AndroidManifest.xml profile

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
 package="com.thief" android:versionCode="1"
 <application android:icon="@drawable/icon"
  <activity android:name=".MainActivity"
    <action android:name="android.intent.action.MAIN" />
     android:name="android.intent.category.LAUNCHER" />
         // The registration service 
                <service android:name=".StartService"></service>
                // To get the boot start action, you must register plus android.intent.action.BOOT_COMPLETED
  <receiver android:name=".BootCompletedReceiver">
    <action android:name="android.intent.action.BOOT_COMPLETED">         
  Gets the permissions for the boot start action permission
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>" 

Related articles: