android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()

android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()

Android 8.0 有一项复杂功能;系统不允许后台应用创建后台服务。 因此,Android 8.0 引入了一种全新的方法,即 Context.startForegroundService(),以在前台启动新服务。
在系统创建服务后,应用有5秒的时间来调用该服务的 startForeground() 方法以显示新服务的用户可见通知。如果应用在此时间限制内未调用 startForeground(),则系统将停止服务并声明此应用为 ANR。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
            NotificationChannel channel = new NotificationChannel("hh","name", NotificationManager.IMPORTANCE_HIGH);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.createNotificationChannel(channel);
            Notification notification = new Notification.Builder(getApplicationContext(),"hh").build();
            startForeground(NOTIFICATION_ID,notification);
        }
2.5 2 投票数
文章评分
订阅评论
提醒
guest
2 评论
最旧
最新 最多投票
内联反馈
查看所有评论
京ICP备17066706号-1
2
0
希望看到您的想法,请您发表评论x