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安装教程(辅助挂)竟然有挂(有挂规律辅助脚本)1、透视辅助软件激活码多个强...
一分钟了解!pokerworl... 一分钟了解!pokerworld修改器(辅助挂)好像是真的有挂(有挂功能辅助技巧)1、实时透视辅助更...
带你了解!hhpoker买挂(... 带你了解!hhpoker买挂(辅助挂)切实是有挂(详细教程辅助脚本)在进入软件靠谱后,参与本局比赛的...
我来向大家传授!wepoker... 我来向大家传授!wepoker俱乐部辅助(辅助挂)总是是有挂(有挂教程辅助脚本)1、脚本辅助下载、免...
免费测试版!约局吧德州真的存在... 免费测试版!约局吧德州真的存在透视吗(辅助挂)原来存在有挂(有挂详细辅助软件)1、用户打开应用后不用...
玩家必看教程!wpk俱乐部有没... 玩家必看教程!wpk俱乐部有没有辅助(辅助挂)总是是有挂(有挂总结辅助器)1、点击下载安装,脚本分类...
揭秘真相!wepoker辅助是... 揭秘真相!wepoker辅助是真的假的(辅助挂)一直存在有挂(有挂神器辅助攻略)1、在插件功能辅助器...
记者揭秘!xpoker辅助控制... 记者揭秘!xpoker辅助控制(辅助挂)确实是真的有挂(有挂实锤辅助器)1、记者揭秘!xpoker辅...
教学盘点!德普之星透视辅助软件... 教学盘点!德普之星透视辅助软件(辅助挂)一贯真的有挂(有挂猫腻辅助技巧)1)辅助插件:进一步探索辅助...
攻略讲解!wepoker透视脚... 攻略讲解!wepoker透视脚本下载(辅助挂)都是是有挂(有挂解惑辅助插件)1、点击下载安装,脚本分...