Sample boot up for android basic tutorial

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

Manifest. xml file:


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

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.app.ActivityManager;
import android.app.Service;
import android.app.ActivityManager.RunningAppProcessInfo;
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;
    }
    @SuppressLint("NewApi")
    public void onCreate() {

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

    }
    private Runnable mTasks = new Runnable() {
        @SuppressLint("NewApi")
        public void run() {
            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND);
            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(
                        "cn.start.test");
                if (intent != null) {
                    DaemonService.this.startActivity(intent);
                    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. ");
                    return;
                }
            }
            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){
            if(ra.processName.equals("cn.start.test")){
                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" />
            </intent-filter>
        </receiver>


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

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

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: