Android10在内存清理时结束前台服务
创始人
2024-08-20 08:30:13
0

在AndroidManifest.xml中,添加以下代码,使应用程序免受最新更新影响:




在对应的Service类中,添加以下代码,以确保前台服务不会在内存清理时被杀死:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        String channelId = "your_channel_id";
        NotificationChannel channel =
                new NotificationChannel(channelId, "Foreground Service Channel",
                        NotificationManager.IMPORTANCE_HIGH);
        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
                .createNotificationChannel(channel);
        Notification notification = new NotificationCompat.Builder(this, channelId)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(getString(R.string.notification_text))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setOngoing(true)
                .build();

        serviceIntent = intent;
        startForeground(1, notification);
        return START_REDELIVER_INTENT;
    } else {
        return super.onStartCommand(intent, flags, startId);
    }
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
        restartServiceIntent.setPackage(getPackageName());

        PendingIntent restartServicePendingIntent =
                PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent,
                        PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmService =
                (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        if (alarmService != null) {
            alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000,
                    restartServicePendingIntent);
        }
        super.onTaskRemoved(rootIntent);
    }
}

以上代码的作用是在Android 10及更高版本上,将前台服务设置为具

相关内容

热门资讯

分享实测!wepoker私人辅... 分享实测!wepoker私人辅助器,wepoker破解器有用吗,一直存在有挂(详细教程)能透视中分为...
玩家必看教程!约局吧如何查看是... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
玩家必看科普!拱趴大菠萝万能辅... 玩家必看科普!拱趴大菠萝万能辅助器,wepoker私人局透视插件,切实是有挂(有挂方法)进入游戏-大...
总算了解!wpk软件是真的吗,... 总算了解!wpk软件是真的吗,wpk是真的还是假的,都是存在有挂(真的有挂);1)辅助插件:进一步探...
热门推荐!aapoker怎么控... 热门推荐!aapoker怎么控制牌,哈糖大菠萝开挂,都是是真的有挂(有挂细节)小薇(辅助器软件下载)...
科技新动态!pokemmo手机... 科技新动态!pokemmo手机脚本,拱趴大菠萝挂哪里,切实存在有挂(有挂方法)1、打开软件启动之后找...
玩家亲测!hhpoker为什么... 玩家亲测!hhpoker为什么一直输,hhpoker辅助器,本来是有挂(证实有挂)1、下载好正确养号...
三分钟了解!wepoker免费... 三分钟了解!wepoker免费透视脚本,约局吧德州可以透视吗,本来是有挂(真的有挂)wepoker免...
查到实测!wepoker好友房... 查到实测!wepoker好友房开挂,wepoker开辅助能查到吗,总是是真的有挂(的确有挂)1、模拟...
分享一款!wepoker轻量版... 分享一款!wepoker轻量版透视,wepokerplus外挂,一直是真的有挂(有挂详细)1、玩家可...