Android高版本后台开机检测自启动service(解决did not then call Service.startForeground())

非android工程师,单纯个人临时有需要,赶时间借鉴很多文章做出来的,单纯记录备忘下。原帖之一如下:

【错误记录】前台进程报错 ( Bad notification for startForeground invalid channel for service notification )_韩曙亮的博客-CSDN博客

1、新建一个Receiver,继承BroadcastRecevier类,用于开机广播,启动service服务。

java 复制代码
/**
 * 接收开机广播来启动service
 */
public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent serviceIntent = new Intent(context, WebSocketService.class);
            // 判断版本
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(context)) {
                    // 有悬浮框权限
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        context.startForegroundService(serviceIntent);
                    } else {
                        context.startService(serviceIntent);
                    }
                } else {
                    //没有悬浮框权限 要去索要悬浮框权限
                    System.out.println("没有悬浮框权限 要去索要悬浮框权限");
                }
            } else {
                //低版本不需要悬浮框权限 直接显示
                context.startService(serviceIntent);
                System.out.println("低版本不需要悬浮框权限 直接显示");
            }
        }
    }
}

2、在AndroidManifest.xml中配置如下:

java 复制代码
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><!-- 接收广播 -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /><!-- 悬浮窗权限 -->
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
java 复制代码
        <!-- 广播 -->
        <receiver
            android:name=".Receiver.BootReceiver"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

3、在service中的oncreated和onDestroy中添加如下:

java 复制代码
    @Override
    public void onCreate() {
        super.onCreate();
        startForeground();
    }

    //关闭长连接
    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
    }

    /**
     * 启动前台服务
     */
    private void startForeground() {
        String channelId = null;
        // 8.0 以上需要特殊处理
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channelId = createNotificationChannel("kim.hsl", "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 小时前
自动化模型管理:MediaPipe Android SDK 中的模型文件下载与加载机制
android·运维·自动化
AgilityBaby4 小时前
Untiy打包安卓踩坑
android·笔记·学习·unity·游戏引擎
硬件学长森哥6 小时前
Android音视频多媒体开源框架基础大全
android·图像处理·音视频
二流小码农6 小时前
鸿蒙开发:CodeGenie万能卡片生成
android·ios·harmonyos
没有了遇见6 小时前
Android 直播间动画动画队列实现
android
月山知了6 小时前
Android有的命令不需要root权限,有的命令需要root权限是如何实现的
android
科技道人8 小时前
Android 实体键盘 设置默认布局
android·实体键盘·设置默认键盘语言
SHUIPING_YANG8 小时前
tp3.1临时连接指定数据库,切片分类in查询,带过滤需要的数据
android·数据库
前端呆猿8 小时前
Vuex:Vue.js 应用程序的状态管理模式
android·vue.js·flutter
望佑8 小时前
Jetpack Compose 入门:从默认工程到实战开发
android