前台服务,即foreground service. 前台服务的优先级是比较高的,android系统极少会主动杀死前台服务。类似音乐播放器后台播放功能,就是需要启动一个前台服务。下面的代码显示了如何启动一个前台服务。
当前台服务在运行时,前台服务可以在通知栏里面被看到,当被选择时,系统会唤醒上面已经定义了的pi提供的activity,即MainActivity,它是一个 pendingintent。
当不使用时,需要关闭服务,调用
stopForeground(true);
结束服务。
前台服务,即foreground service. 前台服务的优先级是比较高的,android系统极少会主动杀死前台服务。类似音乐播放器后台播放功能,就是需要启动一个前台服务。下面的代码显示了如何启动一个前台服务。
当前台服务在运行时,前台服务可以在通知栏里面被看到,当被选择时,系统会唤醒上面已经定义了的pi提供的activity,即MainActivity,它是一个 pendingintent。
当不使用时,需要关闭服务,调用
stopForeground(true);
结束服务。