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

相关内容

热门资讯

为了进一步!aa poker辅... 为了进一步!aa poker辅助包,德扑之星私人局辅助器,一直存在有挂(有挂解惑)德扑之星私人局辅助...
据公告内容!hhpoker破解... 据公告内容!hhpoker破解工具,德普之星透视,一贯是有挂(详细教程)1、德普之星透视模拟器是什么...
2026版总结!wejoker... 2026版总结!wejoker手机版透视脚本,德扑之星透视软件免费入口官网,一直存在有挂(有挂规律)...
2026版复盘!wepoker... 2026版复盘!wepoker透视脚本网页,德扑之星怎么开辅助,果然真的有挂(有挂教程)小薇(辅助器...
目前来看!德普之星辅助器app... 目前来看!德普之星辅助器app,德普之星透视软件免费入口官网,切实是真的有挂(发现有挂)1、下载好德...
教程辅助挂!hhpoker真能... 教程辅助挂!hhpoker真能买到挂吗,德扑之星有辅助软件吗,都是是真的有挂(有挂总结)1、操作简单...
据悉!wepoker透视脚本免... 据悉!wepoker透视脚本免费使用视频,德普辅助软件,好像是真的有挂(有挂方法)1、任何德普辅助软...
代打辅助挂!智星德州辅助译码插... 代打辅助挂!智星德州辅助译码插件靠谱吗,智星德州插件最新版本更新内容详解,一贯存在有挂(竟然有挂)1...
黑科技辅助挂!wepoker网... 黑科技辅助挂!wepoker网页版透视方法,智星德州插件怎么下载,竟然是有挂(证实有挂)1、黑科技辅...
此事备受玩家关注!wepoke... 此事备受玩家关注!wepoker辅助分析器,德普之星透视辅助,竟然确实有挂(有挂方法)一、德普之星透...