Sample boot up for android basic tutorial

  • 2020-05-27 07:04:29
  • OfStack

Manifest. xml file:

            android:process=".DaemonService" >
            <intent-filter android:priority="1000">
                <action android:name="cn.test.DaemonService" />
                <category android:name="android.intent.category.DEFAULT" />

Note: since our service will run in the background, we will not use bindService, but startService.

So that we don't end up with the program, and we end up with the service.

package cn.start.test;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class DaemonService extends Service {
    private static final String TAG = "Alarmreceiver";
    Handler hd1 = new Handler();
    int delay = 5000;

    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    public void onCreate() {

        System.out.println(" Service started successfully... ");
        hd1.postDelayed(mTasks, delay);

    private Runnable mTasks = new Runnable() {
        public void run() {
            if (checkMainAppIsActive()) {
                Log.d(TAG, " The service detection main application is still running ");
            } else {
                Log.d(TAG, " The service detection main application has closed ");
                Intent intent = getPackageManager().getLaunchIntentForPackage(
                if (intent != null) {
                    Log.d(TAG, " The service launches the main application. ");
                } else {
                    Log.d(TAG, " The service detects that the main application is not installed and exits automatically. ");
            hd1.postDelayed(mTasks, delay);


     *  Check if the main program is still running the copyright 
     * @return
    @SuppressLint({ "NewApi", "NewApi" })
    public boolean checkMainAppIsActive(){
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        // Get the running application 
        List<RunningAppProcessInfo> run = am.getRunningAppProcesses();
        for(RunningAppProcessInfo ra : run){
                return true;
        return false;

Start up the automatic startup procedures, nature is not necessary to start up the broadcast.

manifest. xml file:

<receiver android:name=".StartupReceiver" >
            <intent-filter android:priority="1000" >
                <action android:name="android.intent.action.BOOT_COMPLETED" />

public class StartupReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent i = new Intent(context,LoginActivity.class);

        Intent serviceIntent = new Intent(context,DaemonService.class);

Conclusion: start up broadcast 1 must be registered in manifest file. Watch out for timing delays in timers.

Through List < RunningAppProcessInfo > run = am. getRunningAppProcesses (); Determine if your process is still running

Through the getPackageManager (.) getLaunchIntentForPackage (" cn. start. test "); getPackageManager (). getLaunchIntentForPackage (" cn. start. test ");

To start your own program.

Related articles: