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) {
        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'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) {
  if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
   Intent newIntent = new Intent(context,StartService.class);
   context.startService(newIntent);
  } 
 }
}

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);
 }
    @Override 
    public void onCreate() {
  registerIntentReceiver();
    }
}

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


<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 
 android:id="@+id/AbsoluteLayout01" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 xmlns:android="http://schemas.android.com/apk/res/android">
 <Button android: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>

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


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.thief" android:versionCode="1"
 android:versionName="1.0">
 <application android:icon="@drawable/icon"
  android:label="@string/app_name">
  <activity android:name=".MainActivity"
   android:label="@string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category
     android:name="android.intent.category.LAUNCHER" />
   </intent-filter>  
  </activity>
         // 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">
   <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED">         
    </action>
   </intent-filter>
  </receiver>  
 </application>
  Gets the permissions for the boot start action permission
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>" 
</manifest>


Related articles: