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

编程入门 行业动态 更新时间:2024-10-23 14:31:55

Android高版本<a href=https://www.elefans.com/category/jswz/34/1771386.html style=后台开机检测自启动service(解决did not then call Service.startForeground())"/>

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

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

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

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

/*** 接收开机广播来启动service*/
public class BootReceiver extends BroadcastReceiver {@Overridepublic 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中配置如下:

<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" />
        <!-- 广播 --><receiverandroid: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中添加如下:

    @Overridepublic void onCreate() {super.onCreate();startForeground();}//关闭长连接@Overridepublic 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;}

更多推荐

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

本文发布于:2023-11-15 11:45:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1599045.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   版本   Android   service   startForeground

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!