android9.0以上当切换程序后,service在后台运行一分钟左右就自动停止的解决方案

service作为安卓四大组件之一,在开发项目中很多人都会用到,不知道大家有没有发现,在android9.0以上的系统,如果你在app启动了一个service,当你切换到另一个app时,在之前的安卓系统service还是会在运行的,但是当升级到android9.0的系统,你会发现,service在后台运行一分钟左右就会自动停止了,下面来给大家介绍一下如何使service一直在后台运行:

首先我们来了解一下官方文档上android9.0默认关掉后台运行的service的原因:

1、在后台运行应用会消耗设备的有限资源,如内存或者电池电量,进而影响用户的使用体验,比如说,后台任务可能会缩短设备的续航时间,或者在用户玩游戏、看视频或拍照时造成系统卡顿。

2、为了改善续航表现和用户体验,Android 在版本演进的过程中,针对后台执行进行了不同程度的限制,其中包括:

     低耗电模式和应用待机模式: 如果设备未插接电源,处于空闲状态一段时间且屏幕关闭,系统会进入低耗电或者待机模式,并对       应用行为施加相应限制;

    后台位置限制: 对后台应用获取用户当前位置的频率进行限制;

    后台服务限制: 限制应用在后台运行服务,并禁止应用通过隐藏/不可见方式调用 CPU 或网络资源;

   近期新增特性及优化项: (1) 应用待机群组: 限制非活跃应用对设备资源的调用; (2) 后台限制: 当系统检测到应用出现不良行为时,     系统会向用户询问是否要限制该应用在后台访问设备资源; (3) 省电助手功能优化。
 

解决思路:
Service启动时创建一条通知,与其绑定,这样锁屏或者后台Service都不会被暂停或杀死。

创建一个service类,在onCreate方法里执行如下代码:

    private String notificationId = "serviceid";
    private String notificationName = "servicename";
    private void showNotification(){
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //创建NotificationChannel
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel(notificationId, notificationName, NotificationManager.IMPORTAN                                                                                                     CE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }
        startForeground(1,getNotification());
    }
    private Notification getNotification() {
        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("title")
                .setContentText("text");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(notificationId);
        }
        Notification notification = builder.build();
        return notification;
    }

注意要加上这个权限:<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

以上方法就可以使service一直在后台运行了。
 

  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
Android 9.0 及以上版本,为了进一步增强应用程序的安全性,Android 引入了更严格的后台限制,禁止所有未在前台运行的应用程序启动服务。如果您想在后台启动服务,需要使用 `startForegroundService()` 方法,并且需要在 5 秒钟内调用 `startForeground()` 方法将服务设置为前台服务。 以下是一个使用 `startForegroundService()` 和 `startForeground()` 的示例代码: ``` if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 创建一个 NotificationChannel NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT); // 向系统注册 NotificationChannel NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); } // 创建一个 Intent,启动你的服务 Intent serviceIntent = new Intent(this, YourService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 在 Android 9.0 及以上版本上,需要调用 startForegroundService() 方法启动服务。 startForegroundService(serviceIntent); // 在启动服务后,5 秒钟内调用 startForeground() 方法将服务设置为前台服务。 // 如果在 5 秒钟内没有调用 startForeground() 方法,系统会认为服务无法正常启动,会抛出异常。 new Handler().postDelayed(new Runnable() { @Override public void run() { startForeground(1, new Notification()); } }, 5000); } else { // 在 Android 9.0 以下版本上,可以直接调用 startService() 方法启动服务。 startService(serviceIntent); } ``` 注意:在 Android 9.0 及以上版本上,如果你使用的是 `startForeground()` 方法,会在 5 秒钟后抛出 `ANR` 异常,因为 Android 9.0 及以上版本要求服务必须在 5 秒钟内设置为前台服务。如果你想在后台运行服务,必须使用 `startForegroundService()` 和 `startForeground()` 方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值