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

相关内容

热门资讯

分辨真假!闲聚鱼虾蟹软件脚本,... 分辨真假!闲聚鱼虾蟹软件脚本,聚星ai辅助工具下载,靠谱教程(真实有挂)是一款可以让一直输的玩家,快...
实操分享!牵手跑得软件,wep... 实操分享!牵手跑得软件,wepoker高级辅助,详细教程(有挂方略);相信小伙伴都知道这个牵手跑得软...
揭秘关于!人海大厅挂什么好,w... 揭秘关于!人海大厅挂什么好,we poker辅助器v3.3,揭秘攻略(详细教程);1、点击下载安装,...
科技新动态!乐酷大厅怎么安装,... 您好,乐酷大厅怎么安装这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在...
一分钟揭秘!胡乐辅助脚本是真的... 一分钟揭秘!胡乐辅助脚本是真的假的,wpk有作弊吗,安装教程(有挂助手)是一款可以让一直输的玩家,快...
热点推荐!皮皮游戏辅助平台,w... 【福星临门,好运相随】;热点推荐!皮皮游戏辅助平台,wepoker破解游戏盒子,技巧教程(有挂分享)...
交流学习经验!家乡大贰辅助,p... 交流学习经验!家乡大贰辅助,pokerworld辅助器,可靠技巧(有挂技巧);小薇(透视辅助)致您一...
我来教大家!瑞安玉海楼茶苑辅助... 我来教大家!瑞安玉海楼茶苑辅助器,hhpoker怎么开透视,2025新版技巧(真是有挂);最新版20...
我来向大家传授!卡五星辅助器,... 我来向大家传授!卡五星辅助器,wepoker透视脚本免费app,必胜教程(有挂详细);1、点击下载安...
重大科普!途游四川辅助器是真的... 重大科普!途游四川辅助器是真的吗,wepoker有没有挂,安装教程(证实有挂);亲,有的,ai轻松简...