Android 8通过startService引起crash。Bad notification for startForeground:... invalid channel for service

2023-05-16

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)

Android 8.0 不再允许后台service直接通过startService方式去启动,否则就会引起IllegalStateException。解决方式:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(new Intent(context, MyService.class));
    } else {
        context.startService(new Intent(context, MyService.class));
}

然后必须在Myservice中调用startForeground():

@Override
public void onCreate() {
    super.onCreate();
    startForeground(1,new Notification());
}

 注意:在要开启的service中给notification添加 channelId,否则会出现如下错误:

RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid ch......

完整参考代码如下:

    NotificationManager notificationManager;  

    String notificationId = "channelId";    

    String notificationName = "channelName";    

    private void startForegroundService()

    {        

       notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        

      //创建NotificationChannel        

      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            

      NotificationChannel channel = new NotificationChannel(notificationId, notificationName, NotificationManager.IMPORTANCE_HIGH);            

      notificationManager.createNotificationChannel(channel);       

      }        

      startForeground(1,getNotification());    

  }

  private Notification getNotification() {        

    Notification.Builder builder = new Notification.Builder(this)

                 .setSmallIcon(R.drawable.myicon)

                 .setContentTitle("投屏服务")

                 .setContentText("投屏服务正在运行...");   

      //设置Notification的ChannelID,否则不能正常显示        

     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            

        builder.setChannelId(notificationId);        

     }        

    Notification notification = builder.build();        

    return notification;    

  }    

  @Override  

  public void onCreate() {        

    super.onCreate();       

    startForegroundService();   

    FLog.d(TAG, "[onCreate] MediaServer Service Started.");    

  }

补充:关于Android 8.0中Service和Notification

后台执行限制 Android 8.0 为提高电池续航时间而引入的变更之一是,当您的应用进入已缓存状态时,如果没有活动的组件,系统将解除应用具有的所有>>>唤醒锁。 此外,为提高设备性能,系统会限制未在前台运行的应用的某些行为。具体而言: 现在,在后台运行的应用对后台服务的访问受到限制。 应用无法使用其清单注册大部分隐式广播(即,并非专门针对此应用的广播)。 默认情况下,这些限制仅适用于针对 O 的应用。不过,用户可以从 Settings 屏幕为任意应用启用这些限制,即使应用并不是以 O 为目标平台。 Android 8.0 还对特定函数做出了以下变更: 如果针对 Android 8.0 的应用尝试在不允许其创建后台服务的情况下使用 startService() 函数,则该函数将引发一个 IllegalStateException。 新的 Context.startForegroundService() 函数将启动一个前台服务。现在,即使应用在后台运行,系统也允许其调用 Context.startForegroundService()。不过,应用必须在创建服务后的五秒内调用该服务的 startForeground() 函数。

 

转载于:Android 8通过startService引起crash问题 - Johnny9 - 博客园

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 8通过startService引起crash。Bad notification for startForeground:... invalid channel for service 的相关文章

  • 意图?如何注册以获得传入电子邮件提醒?

    这里是极限 Android 开发新手 好吧 Android 开发新手 而不是一般开发 我想编写一个应用程序 当电子邮件进入设备时会收到通知 并根据电子邮件中的数据 主题 发件人 收件人等 采取各种操作 我认为我要做的是创建一个带有 Inte
  • 处理 Android 锁屏上的音量变化?

    我想做的是 能够在 android 4 4 上的锁屏上捕获音量增大 减小按钮操作 Google Cast 设计清单 https developers google com cast docs design checklist sender
  • 如何从一个活动中完成一系列开放的子活动?

    我正在尝试为我的应用程序制作一个退出按钮 无论如何 我能够跟踪我的应用程序中的所有活动实例 然后完成它们 但在某些情况下 仍有一些活动仍然存在 不知道怎么办 有没有什么方法可以杀死android中的特定应用程序 或者我可以通过任何其他方式退
  • 在 Android 中始终以横向模式打开相机

    在我的 Android 应用程序中 单击按钮后我希望相机以横向模式打开 即使我将手机旋转为纵向模式 相机也应始终处于横向模式或纵向模式 使用此代码在横向模式下打开相机 Intent cameraIntent new Intent Media
  • 使用 RxJava 限制吞吐量

    我现在遇到的情况很难解释 所以我会写一个更简单的版本来解释这个问题 我有一个Observable from 它发出一系列由ArrayList文件数量 所有这些文件都应上传到服务器 为此 我有一个函数可以完成这项工作并返回一个Observab
  • 如何更改对话框片段内的片段

    我想做一个空的DialogFragment with a LinearLayout然后更改里面的片段LinearLayout 例如 第一个片段是 3 个按钮 facebook google 电子邮件登录 的登录 当有人按下电子邮件时 第 2
  • 如何在android中画一条曲线?

    我是 Android 新手 正在开发一个关于绘制线条的示例项目 我想画一条连接两点的曲线或高架线 x1 y1 and x2 y2 我试过canvas drawArc 方法 但是RectF内的值drawArc方法只是圆的 x y 中心点 它在
  • 我应该选择的最低 SDK 版本是多少? (截至2018年11月)

    据我所知 android studio 中默认的最小 SDK 设置是 15 我读到我应该增加它 因为没有多少人 或者可能没有 仍在使用该 android 版本 另外 我计划使用 android studio 中的一些新功能 这些功能仅适用于
  • Android 消费品:“已经拥有该商品”,但 inventory.hasPurchase() 为 false

    我被 Google In App v3 困住了 我测试了一次没有消费的购买 例如 当应用程序在购买和消费之间崩溃时 现在我找不到出路 如果我尝试再次购买 它会显示 您已经拥有该商品 但是当我测试所有权时 它说我不拥有它 Inventory
  • Android - 多次实例化一个片段?

    我正在创建一个在 ListView 中显示数据的应用程序 数据分为两种类型 热门 收藏夹 我有一个活动和两个片段 片段根据类别显示项目列表 我为此使用了 ListView 然后我有两个fragment layouts 它们在设计上完全相同
  • 收到“无法解析上传的APK的AndroidManifest.xml。它是否正确编译?”启用 Google 应用签名后出现错误

    启用后谷歌应用程序签名 https support google com googleplay android developer answer 7384423 hl en 每次我尝试将签名版本 APK 上传到 Play 商店时 都会收到一
  • Android httpclient文件上传数据损坏和超时问题

    我在 Android 中上传图像时遇到问题 我正在使用 apache httpmime 4 1 lib 代码是这样的 MultipartEntity reqEntity new MultipartEntity HttpMultipartMo
  • Spotify 登录错误 INVALID_CLIENT:无效的重定向 URI android

    我正在制作一个包含 Spotify 集成的应用程序 我点击了此链接https developer spotify com technologies spotify android sdk tutorial https developer s
  • 使用 UPI url 调用 PSP 应用程序

    我正在尝试创建一个商家应用程序 它将根据 NPCI 的指南生成一个 url 此 url 将作为意图共享 并且 PSP 应用程序 任何注册的银行应用程序 应该能够侦听该 url 并被调用 我已经形成了这样的网址 upi pay pa icic
  • 无法登录 Google Play 游戏服务

    我在开发者控制台上使用包名称和正确的签名证书设置了我的游戏 并为其创建了排行榜 但没有创建任何成就 然后 我从以下位置下载了示例 Type A Number Challenge 和 BaseGameUtils https developer
  • 如何通过 AppCompatActivity 使用 YouTube Android 播放器 API

    为了在我的应用程序中播放视频 我决定扩展 YouTube Android Player API 但问题是我的菜单消失了 因为我没有从 AppCompatActivity 扩展 问题是 如何使用 YouTube Android Player
  • 从 sqlite 和 mysql 加载数据微调器

    我试试这个tutorial http nielpoenya blogspot com 2012 08 tutorial android spinner dari database html加载Spinner from sqlite and
  • 制作弹跳动画

    我想做图层的弹跳动画 我已经完成了该图层从右到中心的操作 现在我想将其向后移动一点 然后回到中心 这会产生反弹效果 我想我可以用这样的翻译来做到这一点
  • Application.onLowMemory() 未调用

    我创建了自己的应用程序类 我尝试调试它 代码在 Application onCreate 处停止 但不会在 onLowMemory 处停止 为了测试该场景 我打开了许多其他高内存应用程序 我看到的是调试会话终止 在 Eclipse 中 并且
  • Android Jasper 报告

    Jasper Reporting 可以集成到 Android 应用程序中吗 我正在尝试从 jrxml 文件生成 PDF CSV 文本和 XLS 报告 但是 我没有看到 Android SDK 支持 net sf jasperreports

随机推荐