异常如下:

E/AndroidRuntime(22959): FATAL EXCEPTION: main
E/AndroidRuntime(22959): Process: com.example.lota, PID: 22959
E/AndroidRuntime(22959): android.app.RemoteServiceException: Bad notification for startForeground
E/AndroidRuntime(22959): 	at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2080)
E/AndroidRuntime(22959): 	at android.app.ActivityThread.access$2800(ActivityThread.java:258)
E/AndroidRuntime(22959): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2306)
E/AndroidRuntime(22959): 	at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(22959): 	at android.os.Looper.loopOnce(Looper.java:233)
E/AndroidRuntime(22959): 	at android.os.Looper.loop(Looper.java:344)
E/AndroidRuntime(22959): 	at android.app.ActivityThread.main(ActivityThread.java:8205)
E/AndroidRuntime(22959): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(22959): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:589)
E/AndroidRuntime(22959): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1071)
I/BLASTBufferQueue(22959): releaseBufferCallbackThunk bufferId:98608154148877 framenumber:1 blastBufferQueue is dead

在这里插入图片描述
解决方法是 在 Android 8.0上 创建一个 NotificationChannel,代码如下

    /**
     * 启动前台服务
     */
    private void startForeground() {
        String channelId = null;
        // 8.0 以上需要特殊处理
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channelId = createNotificationChannel("com.youn", "ForegroundService");
        } else {
            channelId = "";
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
        Notification notification = builder.setOngoing(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(PRIORITY_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(1, notification);
    }

    /**
     * 创建通知通道
     * @param channelId
     * @param channelName
     * @return
     */
    @RequiresApi(Build.VERSION_CODES.O)
    private String createNotificationChannel(String channelId, String channelName){
        NotificationChannel chan = new NotificationChannel(channelId,
                channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        service.createNotificationChannel(chan);
        return channelId;
    }
2 Android 中的 Service

Service 是一种可在后台执行长时间运行操作而不提供界面的应用组件。

服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。

此外,组件可通过绑定到服务与之进行交互,甚至是执行进程间通信 (IPC)。例如,服务可在后台处理网络事务、播放音乐,执行文件 I/O 或与内容提供程序进行交互。

2.1 Android 中的服务分类如下
前台

前台服务执行一些用户能注意到的操作。例如,音频应用会使用前台服务来播放音频曲目。前台服务必须显示通知。即使用户停止与应用的交互,前台服务仍会继续运行。

后台

后台服务执行用户不会直接注意到的操作。例如,如果应用使用某个服务来压缩其存储空间,则此服务通常是后台服务。

绑定

当应用组件通过调用 bindService() 绑定到服务时,服务即处于绑定状态。绑定服务会提供客户端-服务器接口,以便组件与服务进行交互、发送请求、接收结果,甚至是利用进程间通信 (IPC) 跨进程执行这些操作。仅当与另一个应用组件绑定时,绑定服务才会运行。多个组件可同时绑定到该服务,但全部取消绑定后,该服务即会被销毁。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐