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

相关内容

热门资讯

一分钟了解!pokemmo脚本... 一分钟了解!pokemmo脚本辅助器下载(辅助挂)确实真的是有挂(真的有挂)1、完成pokemmo脚...
总算了解!pokerworld... 总算了解!pokerworld软件(辅助挂)果然是真的有挂(有挂方针)1、每一步都需要思考,不同水平...
记者爆料!hhpoker辅助软... 记者爆料!hhpoker辅助软件下载(辅助挂)好像是真的有挂(发现有挂)1、下载好hhpoker辅助...
技术分享!wepoker数据分... 技术分享!wepoker数据分析(辅助挂)确实真的有挂(真是有挂)1、下载好wepoker数据分析正...
详细说明!wepoker破解游... 详细说明!wepoker破解游戏盒子(辅助挂)原来是真的有挂(的确有挂)1、详细说明!wepoker...
玩家科普!聚星ai辅助工具下载... 玩家科普!聚星ai辅助工具下载(辅助挂)好像有挂(有挂攻略)1、下载好聚星ai辅助工具下载正确养号方...
今日公布!德普之星辅助软件(辅... 今日公布!德普之星辅助软件(辅助挂)本来真的有挂(有挂工具)1、在德普之星辅助软件插件功能辅助器技巧...
一分钟揭秘!wpk辅助软件(辅... 一分钟揭秘!wpk辅助软件(辅助挂)总是真的是有挂(有挂攻略)暗藏猫腻,小编详细说明wpk辅助软件破...
分享实测!wepoker辅助器... 分享实测!wepoker辅助器安装包(辅助挂)真是是有挂(有挂功能)wepoker辅助器安装包能透视...
技术分享!hhpoker外挂靠... 技术分享!hhpoker外挂靠谱吗(辅助挂)总是存在有挂(有挂方法)1、hhpoker外挂靠谱吗辅助...