为什么是 foreground 默认启动的 Service 是被标记为 background,当前运行的 Activity 一般被标记为 foreground,也就是说你给 Service 设置了 foreground 那么他就和正在运行的 Activity 类似优先级得到了一定的提高。当让这并不能保证你得 Service 永远不被杀掉,只是提高了他的优先级。
从Android 1.5开始,一个已启动的service可以调用startForeground(int, Notification)将service置为foreground状态,调用stopForeground(boolean)将service置为background状态。
我们会在调用startForeground(int, Notification)传入参数notification,它会在状态栏里显示正在进行的foreground service。background service不会在状态栏里显示。
在Android 1.0中,将一个service置为foreground状态:
setForeground(true);
mNM.notify(id,
notification);
将一个service置为background状态:
mNM.cancel(id);
setForeground(false);
对比看出,在1.0 API中调用setForeground(boolean)只是简单的改变service的状态,用户不会有任何觉察。新API中强制将notification和改变service状态的动作绑定起来,foreground service会在状态栏显示,而background service不会。
Remote service controller & binding
跨进程调用Service。暂时不研究。