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及更高版本上,将前台服务设置为具

相关内容

热门资讯

最新消息!智星菠萝辅助,德普辅... 最新消息!智星菠萝辅助,德普辅助软件,真是是真的有挂(真是有挂)1、每一步都需要思考,不同水平的挑战...
如何分辨真伪!hardrock... 如何分辨真伪!hardrock透视工具,hh poker透视器下载,原来真的是有挂(果真有挂)1、完...
于此同时!德州局透视脚本免费版... 于此同时!德州局透视脚本免费版下载手机版,德扑软件,原来真的是有挂(有挂功能)在进入德扑软件软件靠谱...
今日公布!aapoker破解侠... 今日公布!aapoker破解侠是真的吗,微扑克游戏辅助器,原来真的是有挂(有挂助手)1、进入游戏-大...
攻略辅助挂!wpk有辅助器吗,... 攻略辅助挂!wpk有辅助器吗,德普之星透视辅助软件是真的吗,一贯真的是有挂(竟然有挂)1)德普之星透...
传递经验!wepoker辅助是... 传递经验!wepoker辅助是真的假的,wepoker免费透视脚本,一直有挂(果真有挂)1、wepo...
经核实!哈糖大菠萝开挂,德普之... 经核实!哈糖大菠萝开挂,德普之星透视辅助,其实存在有挂(有挂教程)1、不需要AI权限,帮助你快速的进...
玩家必看科普!pokemmo手... 玩家必看科普!pokemmo手机版脚本免费,wpk模拟器是什么,总是是有挂(有挂猫腻)玩家必看科普!...
第三方技巧!hhpoker破解... 第三方技巧!hhpoker破解工具,德普辅助软件,原来真的有挂(有挂秘笈)1、打开软件启动之后找到中...
总算了解!hhpkoer辅助挂... 总算了解!hhpkoer辅助挂是真的吗,wepoker有机器人吗,其实是真的有挂(有挂攻略)1、完成...