Android 防止service被kill

阅读 124

2023-03-02


最近做的项目中,需要一直运行service,可是,360一旦清理缓存就把我的service给kill掉了,在网上查了好多方法都不管用,最后终于找到一个:

private void initNotify() {
Intent notificationIntent = new Intent(this, LockActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new Notification();
notification.setLatestEventInfo(this, "LockAppsService", "prevent service to be killed",
pendingIntent);
startForeground(1, notification);
}

 

这样就能显示一个前台服务

精彩评论(0)

0 0 举报