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破解工具,其实是真的有挂(有挂解密)1、打开软件启动...
专业讨论!wepoker私人局... 专业讨论!wepoker私人局外卦,werplan脚本,真是是有挂(证实有挂)1、起透看视 辅助软件...
如何分辨真伪!扑克之星辅助,w... 如何分辨真伪!扑克之星辅助,wepoker辅助脚本,好像是真的有挂(有挂技巧)1、全新机制【wepo...
我来向大家传授!拱趴大菠萝机器... 我来向大家传授!拱趴大菠萝机器人,we-poker辅助软件教程,总是存在有挂(有挂细节)1、公共底牌...
揭秘攻略!wepoker破解器... 揭秘攻略!wepoker破解器激活码,hhpoker是真的吗,真是真的有挂(有挂秘诀)1、全新机制【...
玩家必看科普!wepoker私... 玩家必看科普!wepoker私人局俱乐部怎么进,wepoker手机版透视脚本,原来是有挂(有挂教程)...
玩家必备科普!wepoker透... 玩家必备科普!wepoker透视底牌,wpk透视工作室,一直真的是有挂(真的有挂)破解侠是真的助透视...
大神推荐!pokemmo脚本辅... 大神推荐!pokemmo脚本辅助器下载,智星菠萝有挂吗,本来真的是有挂(有挂技术)1、免费辅助多个强...
我来教教你!wepoker有人... 我来教教你!wepoker有人用过吗,wepoker智能辅助插件,原来真的有挂(有挂方法)1、操作简...
科技通报!约局吧德州透视,cl... 科技通报!约局吧德州透视,cloudpoker怎么开挂,总是存在有挂(有挂技巧)1、首先打开辅助器下...