我是靠谱客的博主 繁荣蚂蚁,这篇文章主要介绍Context.startForegroundService() did not then call Service.startForeground()产生Anr,现在分享给大家,希望可以做个参考。
直接上正题1解决办法
//开启服务做兼容处理 Intent intentOne = new Intent(this, BackService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intentOne); }else { startService(intentOne); }
接受服务的时候
public void onCreate() { super.onCreate(); if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){ NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //数字是随便写的“40”, nm.createNotificationChannel(new NotificationChannel("40", "App Service", NotificationManager.IMPORTANCE_DEFAULT)); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "40"); //其中的2,是也随便写的,正式项目也是随便写 startForeground(2 ,builder.build()); } }
最后
以上就是繁荣蚂蚁最近收集整理的关于Context.startForegroundService() did not then call Service.startForeground()产生Anr的全部内容,更多相关Context.startForegroundService()内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复