启动服务报错:Context.startForegroundService() did not then call Service.startForeground():

在Launcher(也可以是任何一个app)中启动其他app服务的时候报错AndroidRuntime: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground():

第一次使用在Launcher中使用startService(intent);启动其他app的服务,这样不能进入服务
会出现

APP in background in null uid

Android 8.0 对特定函数做出了以下变更:
针对 Android 8.0 的应用,在不允许其创建后台服务的情况下使用 startService() 函数,则该函数将引发一个 IllegalStateException错误 。
我是用的解决方法是:
将startService()改为startForegroundService()方式启动,并且在server的代码中调用startForegroundService();这样就是把后台服务变成前台服务启动了
在Launcher中修改代码

    private void startNmvSideBarService() {
        ComponentName chatService = new ComponentName("启动app的包名",
                "启动app的包名.启动的服务类");
        Intent intent = new Intent();
        intent.setComponent(chatService);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(intent);
        }
        else {
            startService(intent);
        }
    }

在这里值得注意的是
Android 8.0 有一项复杂功能;系统不允许后台应用创建后台服务。 因此,Android 8.0 引入了一种全新的方法,即 Context.startForegroundService(),以在前台启动新服务。
在系统创建服务后,应用有5秒的时间来调用该服务的 startForeground() 方法以显示新服务的用户可见通知。如果应用在此时间限制内未调用 startForeground(),则系统将停止服务并声明此应用为 ANR。
但是目前在调用:context.startForegroundService(intent)时报如下ANR,startForegroundService()文档说明在service启动后要调用startForeground()。
简而言之 使用了startForegroundService就一定要使用startForeground()
不然就会报以下错误

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

网上有很多的方法,找了很多都没有用,最后找到了一个博客,直接使用他的代码就可以了,这样启动服务之后不会产生闪退的情况。
1.在xml文件中添加

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

2.在被启动的服务中的OnCreate中添加以下代码

        String ID = "com.xxxx.xxxx";	//这里的id里面输入自己的项目的包的路径
        String NAME = "LEFTBAR";
        Intent intent = new Intent(被启动服务名称.this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationCompat.Builder notification; //创建服务对象
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(ID, NAME, manager.IMPORTANCE_HIGH);
            channel.enableLights(true);
            channel.setShowBadge(true);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            manager.createNotificationChannel(channel);
            notification = new NotificationCompat.Builder(SideToolbarService.this).setChannelId(ID);
        } else {
            notification = new NotificationCompat.Builder(SideToolbarService.this);
        }
        notification.setContentTitle("标题")
                .setContentText("内容")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
        Notification notification1 = notification.build();
        startForeground(1,notification1);//这个就是之前说的startForeground

至于为什么这么写不知道,反正程序和程序员一个能跑就行

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>