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

相关内容

热门资讯

透视脚本!红龙poker有辅助... 透视脚本!红龙poker有辅助吗,wpk有没有脚本,实测发现(有挂教程)1、系统规律教程、辅助透视等...
透视辅助!wpk透视脚本下载,... 透视辅助!wpk透视脚本下载,德普之星辅助器,新2025教程(一贯是有挂)1、这是跨平台的德普之星辅...
透视科技!aapoker透视脚... 透视科技!aapoker透视脚本下载,wpk辅助插件叫什么,分享给玩家(有挂黑科技)1)辅助挂:进一...
透视透视!wpk辅助购买,wp... 透视透视!wpk辅助购买,wpk插件辅助,2025教程(竟然存在有挂);一、wpk辅助购买有挂的是的...
透视ai代打!wepoker透... 透视ai代打!wepoker透视版下载,hhpoker怎么开透视,秘笈分析(有挂攻略)1)辅助挂:进...
透视线上!wepoekr底牌透... 透视线上!wepoekr底牌透视,aapoker透视插件,揭秘攻略(固有是真的有挂);(需添加指定Q...
透视好友!红龙poker有辅助... 透视好友!红龙poker有辅助吗,红龙poker透视工具,玩家必备教程(有挂透明)1、很好的工具软件...
透视透视挂!智星德州菠萝安装,... 透视透视挂!智星德州菠萝安装,hhpoker买挂,技巧教程(一直是真的有挂)1、很好的工具软件,可以...
透视能赢!aapoker透视脚... 透视能赢!aapoker透视脚本下载,德普之星透视辅助软件,每日必看(有挂详情)1、不需要AI权限,...
透视黑科技!智星德州菠萝透视插... 透视黑科技!智星德州菠萝透视插件工具,wpk作弊最怕三个东西,玩家教你(最初是有挂);大神普及一款德...