This article describes the method of Android programming to prevent processes from being killed by third-party software. Share it for your reference, as follows:
During the project test, I found that I pressed the home button to return to the desktop and used 360 to clean the memory. The software was finished. When I entered again, I reported an error. I looked at the log and thought that some places were not controlled well, but I didn’t know what the end of 360 (I haven’t figured out this yet). There will be no errors when using process management and optimized memory in Xiaomi system.
Later, I thought about using Service to prevent the software from being killed. After checking the information, I found that Google's management company has the ForegroundService front-end service, which allows the service to run in the front-end task. The front-end service can be implemented in the service oncreate. Through this method, a notification bar must be sent to let the user know that the service is running.
Notification notification = new Notification(, "Service is enabled", ()); |= Notification.FLAG_NO_CLEAR; =Notification.FLAG_ONGOING_EVENT; Intent notificationIntent = new Intent(this, ); PendingIntent pendingIntent = (this, 0, notificationIntent, 0); (this, "service", "Prevent services from being killed by task manager", pendingIntent); startForeground(ONGOING_NOTIFICATION, notification);
This will keep the service running, but the notification bar cannot be cleared, and it will be killed once it is cleared.
Later, when I was doing a custom Notification, the notification bar did not display the notification. After checking, I found that the service was not killed. So I further studied it and found that only two lines of code can keep the service from being killed, and there will be no notification bar notification code as follows:
Notification notification = new Notification(); startForeground(1, notification);
The complete code is as follows:
public class TestService extends Service { private static final Class[] mStartForegroundSignature = new Class[] { , }; private static final Class[] mStopForegroundSignature = new Class[] { }; private NotificationManager mNM; private Method mStartForeground; private Method mStopForeground; private Object[] mStartForegroundArgs = new Object[2]; private Object[] mStopForegroundArgs = new Object[1]; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { (); mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); try { mStartForeground = ("startForeground", mStartForegroundSignature); mStopForeground = ("stopForeground", mStopForegroundSignature); } catch (NoSuchMethodException e) { mStartForeground = mStopForeground = null; } // We don't need to set FLAG_ONGOING_EVENT for // The front desk service always contains the flag bit by default Notification notification =new Notification(); // Note that using startForeground, if id is 0, the notification will not be displayed. startForegroundCompat(1, notification); } @Override public void onDestroy() { (); stopForegroundCompat(1); } // Start front desk service in a compatible way private void startForegroundCompat(int id, Notification n) { if (mStartForeground != null) { mStartForegroundArgs[0] = id; mStartForegroundArgs[1] = n; try { (this, mStartForegroundArgs); } catch (IllegalArgumentException e) { (); } catch (IllegalAccessException e) { (); } catch (InvocationTargetException e) { (); } return; } (id, n); } // Stop front desk service in a compatible manner private void stopForegroundCompat(int id) { if (mStopForeground != null) { mStopForegroundArgs[0] = ; try { (this, mStopForegroundArgs); } catch (IllegalArgumentException e) { (); } catch (IllegalAccessException e) { (); } catch (InvocationTargetException e) { (); } return; } // Call cancel before setForeground because we may cancel the foreground service // The moment I was killed. At this time, notification will never be removed from the notification column (id); } }
After testing, 360 mobile assistant and Tencent mobile phone manager cannot kill the service, but after manually finishing it, I opened it again and found that the audio was still playing (the client related to audio), which felt a little awkward.
I hope this article will be helpful to everyone's Android programming design.