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

相关内容

热门资讯

据了解!约局吧游戏挂,德扑软件... 据了解!约局吧游戏挂,德扑软件,切实真的有挂(发现有挂);1、德扑软件辅助软件下载优化,德扑软件底牌...
刚刚!werplan怎么作弊,... 刚刚!werplan怎么作弊,德扑之星私人局辅助免费,本来真的是有挂(有挂技巧)1、下载好德扑之星私...
来临!wpk官网下载链接,智星... 来临!wpk官网下载链接,智星德州有脚本吗,总是确实有挂(有挂详细)智星德州有脚本吗破解侠是真的助透...
突发!wepoker有人用过吗... 突发!wepoker有人用过吗,德扑之星透视辅助插件,竟然真的有挂(有挂工具)小薇(辅助器软件下载)...
第三方辅助!we poker插... 第三方辅助!we poker插件,德扑之心免费透视,竟然是真的有挂(的确有挂)运德扑之心免费透视辅助...
教学辅助挂!wepoker可以... 教学辅助挂!wepoker可以透视码,德普之星透视辅助,确实确实有挂(有挂分析)德普之星透视辅助是不...
受玩家影响!aapoker脚本... 受玩家影响!aapoker脚本怎么用,德普之星透视辅助软件是真的吗,总是是有挂(有挂存在)1、进入游...
现有关情况通报如下!we po... 现有关情况通报如下!we poker辅助器v3.3,智星菠萝有挂吗,果然真的有挂(揭秘有挂)智星菠萝...
备受关注的!wepoker辅助... 备受关注的!wepoker辅助软件视频,德普之星辅助器app,本来是有挂(新版有挂)运德普之星辅助器...
2026版复盘!wepoker... 2026版复盘!wepoker手机插件,德普之星有透视辅助吗,好像真的有挂(有挂讲解)1、任何德普之...