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是真的还是假的,菠萝辅助器免费版的特点,安装教程(有挂方法)科技教程也叫必备...
玩家必看科普!德普之星app安... 此外,数据分析德州(德普之星app安卓版破解版)辅助神器app还具备辅助透视行为开挂功能,通过对客户...
记者揭秘!aapoker发牌逻... 记者揭秘!aapoker发牌逻辑,哈糖大菠萝软件下载,可靠技巧(有挂透视);1分钟了解详细教程(微信...
大家学习交流!wepoker线... 大家学习交流!wepoker线上大神,wpk私人辅助,科技教程(有挂方法)1、点击下载安装,微扑克w...
必备攻略!wepoker智能辅... 必备攻略!wepoker智能辅助插件,wepoker辅助透视,2025新版教程(有挂攻略);原来确实...
玩家必备教程!wepoker国... 玩家必备教程!wepoker国外版透视,wepoker怎么挂底牌,解密教程(有挂辅助);人气非常高,...
重大发现!wepoker新号好... 1、重大发现!wepoker新号好一点吗,wepokerplus辅助,详细教程(有挂辅助)(UU p...
专业讨论!wepoker怎么获... 此外,数据分析德州(wepoker辅助器软件下载)辅助神器app还具备辅助透视行为开挂功能,通过对客...
必备攻略!wepoker国外版... 必备攻略!wepoker国外版透视,wepoker透视底牌,介绍教程(有挂攻略)科技教程也叫必备教程...
一分钟了解!wpk刷入池率脚本... 一分钟了解!wpk刷入池率脚本,大菠萝辅助器,必赢方法(有挂透明);wpk刷入池率脚本软件透明挂更新...