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真的有透视吗(辅助挂)其实有挂(新版有挂辅助工具)1、每一步都需要思考,不同...
揭秘攻略!智星德州插件最新版本... 揭秘攻略!智星德州插件最新版本更新内容详解(辅助挂)都是是有挂(详细教程辅助技巧)能透视中分为三种模...
实操分享!约局吧德州有挂吗(辅... 实操分享!约局吧德州有挂吗(辅助挂)本来真的有挂(有挂秘籍辅助攻略)1、玩家可以在线上大神俱乐部对游...
玩家攻略!hhpoker作弊码... 玩家攻略!hhpoker作弊码(辅助挂)其实真的有挂(有挂神器辅助技巧)1、点击下载安装,脚本分类一...
查到实测辅助!hhpoker到... 查到实测辅助!hhpoker到底可以作弊吗(辅助挂)切实有挂(竟然有挂辅助软件)1、破解器简单,机器...
推荐十款!wepoker游戏的... 推荐十款!wepoker游戏的安装教程(辅助挂)切实是真的有挂(竟然有挂辅助脚本)1、上手简单,内置...
必备攻略!hhpkoer辅助器... 必备攻略!hhpkoer辅助器(辅助挂)都是真的有挂(揭秘有挂辅助技巧)辅助器是一种具有地方特色的麻...
总算了解!wepoker有辅助... 总算了解!wepoker有辅助器吗(辅助挂)切实存在有挂(有挂方法辅助软件)1、金币登录送、破产送、...
记者爆料!wepoker开辅助... 记者爆料!wepoker开辅助能查到吗(辅助挂)切实是有挂(有挂解密辅助方法)1、游戏颠覆性的策略玩...
重大来袭!wejoker私人辅... 重大来袭!wejoker私人辅助软件(辅助挂)都是真的有挂(有挂秘笈辅助攻略)破解侠是真的助透视。每...