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私人局有透视吗,实用技巧(有挂详细);蜀山四川小...
查到实测辅助!乐胡陇南摆叫辅助... 查到实测辅助!乐胡陇南摆叫辅助器,河南微乐小程序辅助器免费(本来真的有挂)1、点击下载安装,河南微乐...
一分钟揭秘!决战卡五星看牌器,... 一分钟揭秘!决战卡五星看牌器,wepoker怎么设置透视,曝光教程(有挂解密);一分钟揭秘!决战卡五...
指导大家!微信边锋辅助挂件,w... 指导大家!微信边锋辅助挂件,wepoker俱乐部开挂(原来有挂)1、微信边锋辅助挂件辅助软件下载优化...
科技新动态!广西八一字牌可以破... 科技新动态!广西八一字牌可以破解码,乐胡陇南摆叫辅助器(真是有挂)1、任何广西八一字牌可以破解码透视...
我来教大家!皮皮透视辅助软件工... 我来教大家!皮皮透视辅助软件工具,wepoker透视脚本视频,2025新版技巧(有挂秘籍);皮皮透视...
研究成果!多乐跑胡子辅助,we... 您好,多乐跑胡子辅助这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这...
科技通报!欢乐达人猜猜乐辅助,... 科技通报!欢乐达人猜猜乐辅助,雀神广东定制插件(果然有挂)欢乐达人猜猜乐辅助能透视中分为三种模型:欢...
带你了解!广东雀神挂件脚本开挂... 您好:广东雀神挂件脚本开挂这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...