How to keep Android service alive

Supporting the original: http://tryenough.com/android-...

Two aspects can be taken into consideration for the life guarantee Service:

I. ways to change the Service itself

1. Improve Service priority

In the Android manifest.xml file, for the intent filter, you can set the highest priority through the android:priority = "1000" property. 1000 is the highest value. If the number is smaller, the priority will be lower. It is also suitable for broadcasting.

        <service
            android:name="com.dbjtech.acbxt.waiqin.UploadService"
            android:enabled="true" >
            <intent-filter android:priority="1000" >
                <action android:name="com.dbjtech.myservice" />
            </intent-filter>
        </service>

2. Restart when the Service is about to be destroyed

Supporting the original: http://tryenough.com/android-...

You can start service directly in onDestroy()

    @Override
    public void onDestroy() {
 
         Intent sevice = new Intent(this, MainService.class);
         this.startService(sevice);
 
        super.onDestroy();
    }

You can also use service +broadcast to start:

In the onDestroy method, restart the service. When the service goes on onDestroy, send a customized broadcast. When the broadcast is received, restart the service;

        <receiver android:name="com.dbjtech.acbxt.waiqin.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="com.dbjtech.waiqin.destroy" />//This is the custom action
            </intent-filter>
        </receiver>

In onDestroy:

    @Override
    public void onDestroy() {
        stopForeground(true);
        Intent intent = new Intent("com.dbjtech.waiqin.destroy");
        sendBroadcast(intent);
        super.onDestroy();
    }

In the BootReceiver

Supporting the original: http://tryenough.com/android-...

public class BootReceiver extends BroadcastReceiver {
 
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.dbjtech.waiqin.destroy")) {
            //TODO
            //Write the operation related to restarting the service here
                startUploadService(context);
        }
 
    }
 
}

3.onStartCommand method, return START_STICKY

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        flags = START_STICKY;
        return super.onStartCommand(intent, flags, startId);
    }

4.Promote service Process priority
stay onStartCommand Add the following code to the method:

         Notification notification = new Notification(R.drawable.ic_launcher,
         getString(R.string.app_name), System.currentTimeMillis());
        
         PendingIntent pendingintent = PendingIntent.getActivity(this, 0,
         new Intent(this, AppMain.class), 0);
         notification.setLatestEventInfo(this, "uploadservice", "Please keep the program running in the background",
         pendingintent);
        startForeground(0x111, notification);

Methods of utilizing system characteristics

Supporting the original: http://tryenough.com/android-...

1. Special events of monitoring system

Monitor and capture the system's broadcast, such as mobile phone restart, interface wake-up, application status change, etc., and then judge whether our Service is still alive. Don't forget to add permissions.

        <receiver android:name="com.dbjtech.acbxt.waiqin.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="android.intent.action.PACKAGE_RESTARTED" />
                <action android:name="com.dbjtech.waiqin.destroy" />
            </intent-filter>
        </receiver>

In BroadcastReceiver:

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            System.out.println("The phone is on....");
            startUploadService(context);
        }
        if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
                startUploadService(context);
        }
    }

2. Special mobile phone monitoring special push, such as Xiaomi mobile phone registration Xiaomi push

Supporting the original: http://tryenough.com/android-...

Keywords: Android Mobile xml

Added by tommynanda on Thu, 05 Dec 2019 07:53:46 +0200