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

相关内容

热门资讯

大神推荐!博乐填大坑辅助器通用... 大神推荐!博乐填大坑辅助器通用版(外挂辅助工具)详细教程(有挂透明)-哔哩哔哩关于博乐填大坑辅助器通...
免费测试版!中至上饶小程序有挂... 免费测试版!中至上饶小程序有挂的(外挂辅助插件)详细教程(有挂透明)-哔哩哔哩;亲,其实确实真的有挂...
总算了解!创思维激k辅助器(外... 总算了解!创思维激k辅助器(外挂辅助软件)详细教程(有挂透明)-哔哩哔哩;一、创思维激k辅助器AI软...
指导大家!蜜瓜大厅有挂的(外挂... 指导大家!蜜瓜大厅有挂的(外挂辅助神器)详细教程(有挂技巧)-哔哩哔哩;原来确实真的有挂(需添加指定...
透明肯定!开元辅助器下载(外挂... 透明肯定!开元辅助器下载(外挂辅助APP)详细教程(有挂透明)-哔哩哔哩是一款可以让一直输的玩家,快...
一分钟了解!大晋斗地主随意选牌... 自定义大晋斗地主随意选牌系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不...
专业讨论!aapoker下载教... 专业讨论!aapoker下载教程(外挂辅助器)详细教程(有挂方法)-哔哩哔哩;一、aapoker下载...
一分钟揭秘!八闽十三水有外挂的... 一分钟揭秘!八闽十三水有外挂的(外挂辅助工具)详细教程(有挂方法)-哔哩哔哩1、许多玩家不知道八闽十...
透明科技!对战互娱有挂的(外挂... 此外,数据分析德州(对战互娱有挂的)辅助神器app还具备辅助透视行为开挂功能,通过对客户对战互娱有挂...
今日重大通报!青龙辅助软件(外... 今日重大通报!青龙辅助软件(外挂辅助挂)详细教程(有挂教程)-哔哩哔哩;玩家在青龙辅助软件中需先进行...