Android 处理 android.app.RemoteServiceException: Bad notification for startForeground: java.lang.Ru

在做第一行代码的10.5.1节中会出现如下的报错

Android 处理 android.app.RemoteServiceException: Bad notification for startForeground: java.lang.Ru_第1张图片
书上的源码如下
先在MainActivity中定义一个按钮,按钮中的点击事件如下

@Override
    public void onClick(View v) {
        //启动服务时实际上是一个跳转
        switch (v.getId()) {
            case R.id.button1:
                Intent startIntent = new Intent(this,MyService.class);
                //startService(startIntent);  //启动服务
                bindService(startIntent,connection,BIND_AUTO_CREATE);   //绑定服务
                break;
            case R.id.button2:
                //Intent stopIntent = new Intent(this,MyService.class);
                //stopService(stopIntent);   //停止服务
                unbindService(connection);
                break;
        }
    }

在MyService中onCreate的码如下

 @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "新建服务");
        Intent intent = new Intent(MyService.this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("标题")
                .setContentText("内容")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                        R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1,notification);
    }

运行到真机上就会出现闪退,打印日志在最上面,而运行在模拟器上却不会出现闪退的问题。
Android 处理 android.app.RemoteServiceException: Bad notification for startForeground: java.lang.Ru_第2张图片
模拟器能运行而真机不能运行就说明是Android版本的问题。因为我的模拟器是Android5.0版的。使用Notificatio通知再Android8.0以上的的通知要设置渠道,否则就无法显示。关于Notificatio的使用可以参考我以前的博客Notification通知及NotificationChannel的使用。
于是修改了一下MyService中onCreate的代码,代码如下:

 @Override
    public void onCreate() {
        super.onCreate();
        String ID = "com.example.service1";	//这里的id里面输入自己的项目的包的路径
        String NAME = "Channel One";
        Intent intent = new Intent(MyService.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(MyService.this).setChannelId(ID);
        } else {
            notification = new NotificationCompat.Builder(MyService.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);
        //manager.notify(1,notification1);
    }

最后运行到手机上,效果如下

这样就解决了Android版本的问题。
第一行代码第二版中的10.5.1节的实例就这样完成啦。

你可能感兴趣的:(笔记)