【错误记载】Android 悬浮窗开辟报错 ( Context.startForegroundService()
一、错误记载Android 悬浮窗应用 , 启动后 , 成功弹出悬浮窗 , 但是几秒钟之后 , 就会报如下错误 :
2025-02-20 09:40:25.894 22764-22764/hsl.floatingwindow E/AndroidRuntime: FATAL EXCEPTION: main
Process: hsl.floatingwindow, PID: 22764
android.app.ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{d586da8 u0 hsl.floatingwindow/.FloatingWindowService}
at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2596)
at android.app.ActivityThread.access$4000(ActivityThread.java:313)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:3013)
at android.os.Handler.dispatchMessage(Handler.java:117)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:293)
at android.app.ActivityThread.loopProcess(ActivityThread.java:9986)
at android.app.ActivityThread.main(ActivityThread.java:9975)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:586)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1240)
https://i-blog.csdnimg.cn/direct/7e0adaf78b4849b79b2c63f4bb81e6cd.png
Android 应用启动后 , 成功弹出悬浮窗 , 悬浮窗可随意拖动 , 等候约莫 5 秒 , 应用程序瓦解退出 , 报上述错误 ;
https://i-blog.csdnimg.cn/direct/1b90cba4789541f98abedaf512cc5bfa.gif
二、启动服务分析
使用 startService 函数 启动 后台服务 :
[*]该函数用于启动一个平凡的后台服务 ;
[*]服务一旦启动,会持续运行,直到调用 stopSelf() 或 stopService() 方法来停止服务。
[*]启动的是后台服务 , 不需要显示通知 ;
[*]适用于那些不需要与用户进行交互、且不需要长时间在后台运行的使命。
使用 startForegroundService 函数 启动 前台服务 :
[*]该函数专门用于启动 前台服务,该方法是在Android 8.0引入的 ;
[*]特别注意 : 前台服务需要在启动后短时间内(通常是5秒内)调用startForeground(int id, Notification notification)方法,以显示一个持续通知,告知用户该服务正在运行。
[*]适用于那些需要在后台长时间运行、且需要持续通知用户的使命,如音乐播放、文件下载等。
Android 8.0+ 建议 通过调用 startForegroundService 函数 启动前台服务 ;
Android 8.0+ 如果应用在后台运行 , 使用 startService 启动服务大概会失败 , 并抛出IllegalStateException。这主要是为了减少后台运行的服务对系统资源的消耗。
[*]在前台 使用 startService 启动 服务 , 基本不会失败 ;
三、解决方案
在 Android 开辟中 , 使用 startForegroundService 函数启动 前台服务 , 在 前台服务 中 显示悬浮窗时碰到 android.app.ForegroundServiceDidNotStartInTimeException 非常 , 通常是由于服务未在规定时间(5秒)内调用 startForeground() 方法 ;
前台服务 需要确保 在 AndroidManifest.xml 中 声明服务 并 添加前台权限 ;
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application>
<service
android:name=".MyForegroundService"
android:enabled="true"
android:exported="false" />
</application>
在服务的 onStartCommand() 或 onCreate() 方法中立即调用 startForeground(),制止任何耗时操作壅闭主线程。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 立即创建通知并启动前台服务
Notification notification = buildNotification();
startForeground(NOTIFICATION_ID, notification);
// 其他初始化操作放在后台线程
new Thread(() -> initFloatingWindow()).start();
return START_STICKY;
}
此外在启动服务前 , 确保已创建通知渠道 :
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"悬浮窗服务",
NotificationManager.IMPORTANCE_LOW
);
channel.setDescription("用于显示悬浮窗的前台服务");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
在 Activity / Service / Application 中调用此 createNotificationChannel 创建通知渠道 方法 , 如在 onCreate() 方法中 创建 :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createNotificationChannel();
// 启动服务
startForegroundService();
}
创建完通知渠道后 , 再创建通知 :
private Notification buildNotification() {
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("服务运行中")
.setContentText("正在显示悬浮窗")
.setSmallIcon(R.drawable.ic_notification) // 确保资源存在
.setPriority(NotificationCompat.PRIORITY_LOW)
.build();
}
末了 , 调用 startForeground 启动通知 ;
startForeground(NOTIFICATION_ID, notification);
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]