Android programming a way to prevent a process from being killed by third party software
- 2020-10-07 18:52:42
- OfStack
This article gives an example of how Android programming prevents processes from being killed by third party software. To share for your reference, the details are as follows:
When testing the project, I found that pressing home key to return to the desktop and then using 360 to clean up the memory, the software was finished. When entering again, an error was reported. I looked at log, thinking that some places were not well controlled, but I did not know what 360 ended (this is not yet understood). Using the millet system process management optimization memory will not report an error.
Later, I thought of using Service to prevent the software from being dropped by kill. I checked the data and found that the google manager had the foreground service, ForegroundService, so that service 1 could run in the way of previous desk tasks. The foreground service could be realized in oncreate of service.
Notification notification = new Notification(R.drawable.icon, " Service is open ", System.currentTimeMillis());
notification.flags|= Notification.FLAG_NO_CLEAR;
notification.flags=Notification.FLAG_ONGOING_EVENT;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "service", " Prevents services from being killed by the task manager ", pendingIntent);
startForeground(ONGOING_NOTIFICATION, notification);
This keeps service running, but the notification cannot be cleared and the 1 is cleared by kill.
Later, when I did the custom Notification, the notification bar did not show up, and I found that service was not included in kill. So I went one step further and found that it took only two lines of code to keep the service from kill, and there was no notification bar code:
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[] {
int.class, Notification.class };
private static final Class[] mStopForegroundSignature = new Class[] { boolean.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() {
super.onCreate();
mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
try {
mStartForeground = TestService.class.getMethod("startForeground",
mStartForegroundSignature);
mStopForeground = TestService.class.getMethod("stopForeground",
mStopForegroundSignature);
} catch (NoSuchMethodException e) {
mStartForeground = mStopForeground = null;
}
// We don't have to notification.flags Set up the FLAG_ONGOING_EVENT Because the
// Front desk service notification.flags Always include that flag bit by default
Notification notification =new Notification();
// Pay attention to use startForeground . id for 0 Will not show notification
startForegroundCompat(1, notification);
}
@Override
public void onDestroy() {
super.onDestroy();
stopForegroundCompat(1);
}
// Start the front desk service in a compatible manner
private void startForegroundCompat(int id, Notification n) {
if (mStartForeground != null) {
mStartForegroundArgs[0] = id;
mStartForegroundArgs[1] = n;
try {
mStartForeground.invoke(this, mStartForegroundArgs);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return;
}
mNM.notify(id, n);
}
// Discontinue front desk service in a compatible manner
private void stopForegroundCompat(int id) {
if (mStopForeground != null) {
mStopForegroundArgs[0] = Boolean.TRUE;
try {
mStopForeground.invoke(this, mStopForegroundArgs);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return;
}
// in setForeground Before the call cancel Because we may cancel the front desk service
// the 1 Moment be kill It off. At this time notification Will never from notice 1 Column to remove
mNM.cancel(id);
}
}
After testing, neither 360's mobile assistant nor Tencent's mobile butler could kill this service. However, after manual operation, I found that the audio was still playing (audio related client) when I opened it again. I felt a little awkward
I hope this article has been helpful in Android programming.