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

相关内容

热门资讯

据公告内容!hhpoker外挂... 据公告内容!hhpoker外挂靠谱吗,红龙poker辅助,确实是有挂(有挂规律)1、下载好红龙pok...
推荐一款!胡乐情怀宜春外挂,凑... 推荐一款!胡乐情怀宜春外挂,凑一桌开挂游戏辅助,详细软件(有挂透明挂)1、下载好凑一桌开挂游戏辅助脚...
黑科技技巧!xpoker辅助助... 黑科技技巧!xpoker辅助助手,德扑之心免费透视,一直是真的有挂(有挂辅助)1、德扑之心免费透视免...
重大来袭!麻辣竞技辅助,同乡游... 重大来袭!麻辣竞技辅助,同乡游有辅助软件吗,详细教程(有挂技术)1、同乡游有辅助软件吗免费辅助多个强...
今日!newpoker脚本,德... 今日!newpoker脚本,德扑之星辅助器,其实确实有挂(有挂解惑)暗藏猫腻,小编详细说明德扑之星辅...
2026版辅助挂!wepoke... 2026版辅助挂!wepoker模拟器哪个好用,德普之星透视,真是真的是有挂(有挂技巧)1、进入游戏...
推荐几款新版!微信海豚大厅辅助... 推荐几款新版!微信海豚大厅辅助,麻友圈插件,详细软件(有挂教学)1、任何微信海豚大厅辅助透视是真的假...
2026版辅助挂!wepoke... 2026版辅助挂!wepoker透视脚本是什么,智星德州可以透视吗,其实真的有挂(有挂存在)1、任何...
记者爆料!打罗松必胜技巧,随意... 记者爆料!打罗松必胜技巧,随意玩有没有辅助,详细神器(有挂攻略)1、每一步都需要思考,不同水平的挑战...
第三方辅助挂!xpoker怎么... 第三方辅助挂!xpoker怎么作弊,德普之星透视辅助插件,本来真的有挂(真实有挂)1)德普之星透视辅...