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

相关内容

热门资讯

科技通报!pokerrrr2辅... 科技通报!pokerrrr2辅助(辅助挂)其实是真的有挂(有挂存在辅助插件)1、打开软件启动之后找到...
盘点一款!wepoker怎么开... 盘点一款!wepoker怎么开辅助(辅助挂)一贯真的有挂(发现有挂辅助攻略);1、下载好脚本下载之后...
玩家实测!we-poker辅助... 玩家实测!we-poker辅助(辅助挂)其实存在有挂(有挂猫腻辅助app)1、许多玩家不知道辅助怎么...
教学盘点!wepoker手机助... 教学盘点!wepoker手机助手(辅助挂)好像是真的有挂(详细教程辅助软件)1、实时透视辅助更新:用...
我来教大家!hhpoker德州... 我来教大家!hhpoker德州牛仔视频(辅助挂)一直有挂(有挂分析辅助app)1、起透看视 辅助软件...
技术分享!hhpoker是正品... 技术分享!hhpoker是正品吗(辅助挂)本来真的是有挂(存在有挂辅助攻略)1、操作简单,无需手机版...
分辨真假!拱趴大菠萝十三水作弊... 分辨真假!拱趴大菠萝十三水作弊(辅助挂)原来存在有挂(有挂教学辅助工具)1、实时透视辅助更新:用户可...
科技通报!aapoker辅助器... 科技通报!aapoker辅助器是真的吗(辅助挂)都是真的是有挂(有挂规律辅助软件)1、每一步都需要思...
新手必备!有没有人wepoke... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
教程辅助!wepoker透视功... 教程辅助!wepoker透视功能下载(辅助挂)真是是真的有挂(有挂透视辅助工具)1)辅助插件:进一步...