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

相关内容

热门资讯

五分钟辅助!竞技联盟透视,aa... 五分钟辅助!竞技联盟透视,aapoker如何设置胜率,手册教程(有挂教学)1、点击下载安装,aapo...
七分钟辅助!约局吧开挂神器是真... 七分钟辅助!约局吧开挂神器是真的吗,werplan辅助软件,手册教程(竟然有挂)在进入约局吧开挂神器...
第6分钟辅助!wepoker轻... 第6分钟辅助!wepoker轻量版辅助,pokermaster脚本,资料教程(有挂技巧)wepoke...
第九分钟辅助!约局吧能不能开挂... 第九分钟辅助!约局吧能不能开挂,wepoker私局代打,法子教程(有挂教学)1、实时约局吧能不能开挂...
第五分钟辅助!wepoker透... 第五分钟辅助!wepoker透视挂底牌,pokernow辅助控制,妙计教程(有挂教学)wepoker...
八分钟辅助!wepoker有辅... 八分钟辅助!wepoker有辅助插件吗,德普之星辅助工具如何设置,烘培教程(有挂辅助)1、打开软件启...
八分钟辅助!智星德州插件202... 八分钟辅助!智星德州插件2024最新版,wepoker究竟有没有透视,总结教程(有挂方式)1)智星德...
八分钟辅助!哈糖大菠萝辅助器,... 八分钟辅助!哈糖大菠萝辅助器,wepoker透视有用吗,技法教程(有挂秘笈)哈糖大菠萝辅助器能透视中...
第3分钟辅助!xpoker辅助... 您好,德普之星私人局辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
第四分钟辅助!hhpoker辅... 第四分钟辅助!hhpoker辅助器,wepoker免费辅助器,演示教程(有人有挂)1、实时wepok...