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

在做第一行代码的10.5.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版本的问题。因为我的模拟器是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节的实例就这样完成啦。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值