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

相关内容

热门资讯

插件辅助挂!wepoker私人... 插件辅助挂!wepoker私人局俱乐部,德扑之星辅助工具如何打开,本来确实有挂(有挂工具)亲,关键说...
2026版方法!hhpoker... 2026版方法!hhpoker软件安装包,德扑之星私人局辅助免费,竟然是有挂(真的有挂)德扑之星私人...
更值得关注的是!aapoker... 更值得关注的是!aapoker辅助插件工具,如何下载德普之星辅助软件,切实是有挂(有挂技巧)1、用户...
值得注意的是!德普辅助软件,德... 值得注意的是!德普辅助软件,德普之星有透视辅助吗,都是真的有挂(有挂细节)1、德普之星有透视辅助吗脚...
黑科技技巧!wepoker私人... 黑科技技巧!wepoker私人局透视方法,德普辅助软件,都是存在有挂(真实有挂)1、实时德普辅助软件...
据报道!德州圈脚本,智星菠萝透... 据报道!德州圈脚本,智星菠萝透视,切实真的有挂(证实有挂)1、智星菠萝透视免费辅助多个强度级别选择2...
软件辅助挂!德普之星透视软件免... 软件辅助挂!德普之星透视软件免费入口官网,德普之星私人局辅助器,真是是真的有挂(有挂透明挂)1、完成...
随着!拱趴大菠萝挂,智星德州有... 随着!拱趴大菠萝挂,智星德州有脚本吗,一贯是有挂(有挂秘诀);1、随着!拱趴大菠萝挂,智星德州有脚本...
据权威媒体报道!德普之星有透视... 据权威媒体报道!德普之星有透视辅助吗,德扑圈透视挂,都是是有挂(有挂功能)1、该软件可以轻松地帮助玩...
围绕透视问题!werplan外... 围绕透视问题!werplan外挂,德扑之星私人局辅助免费,一直真的有挂(有挂细节)德扑之星私人局辅助...