Service in startForeground (int id, Notification notification) explanation (for the process keep alive)

The official explanation of the method

/**
* If your service is started (running through {@link Context#startService(Intent)}), then
* also make this service run in the foreground, supplying the ongoing
* notification to be shown to the user while in this state.
* By default started services are background, meaning that their process won't be given
* foreground CPU scheduling (unless something else in that process is foreground) and,
* if the system needs to kill them to reclaim more memory (such as to display a large page in a
* web browser), they can be killed without too much harm.  You use
* {@link #startForeground} if killing your service would be disruptive to the user, such as
* if your service is performing background music playback, so the user
* would notice if their music stopped playing.
*
* <p>Note that calling this method does <em>not</em> put the service in the started state
* itself, even though the name sounds like it.  You must always call
* {@link #startService(Intent)} first to tell the system it should keep the service running,
* and then use this method to tell it to keep it running harder.</p>
*
* @param id The identifier for this notification as per
* {@link NotificationManager#notify(int, Notification)
* NotificationManager.notify(int, Notification)}; must not be 0.
* @param notification The Notification to be displayed.
* 
* @see #stopForeground(boolean)
* 
* 翻译:如果服务(通过{@link Context#startService(Intent)}运行行)已经启动,那么还可以让这个服务运行在前台,并提供给用户的提供一个通知。可以调用这个方法
* 默认情况下,服务启动后是处于后台的,这意味着不会被前台CPU调度(除非该服务的一部分运行在前台)。如果系统需要杀死他们去释放内存(比如,需要在浏览器中展示一个大的界面),他们是很容易被杀死。
* 如果终止该服务会对用户造成破坏,那么可以调用该方法。比如:该服务正在播放一段背景音乐,用户是不希望停止掉的
* 
* 注意:调用该方法并不能启动服务,你必须调用startService方法去通知系统让该服务保持运行,然后调用该方法告诉系统更坚固的运行该服务
*/
public final void startForeground(int id, Notification notification) {
     try {
        mActivityManager.setServiceForeground(
                    new ComponentName(this, mClassName), mToken, id,
                    notification, 0);
     } catch (RemoteException ex) {
     }
}
  1. If the service (via {@link Context # startService (Intent)} Run line) has started, it can also make the service running in the foreground, and provided to the user to provide a notification. You can call this method
  2. By default, the service is started in the background, which means that will not be the foreground CPU scheduling (unless run part of the service at the front desk). If the system needs to kill them to free up memory (for example, need to show a big screen in a browser), they are very easy to kill.
  3. If the user terminates the service would cause damage, you can call the method. For example: The service period of the background music is playing, you do not want to stop off

    Note: Calling this method does not start the service, you must call startService ways to make the service notification system to keep running, and then call the method to tell the system more robust run the service

Role: Using this method allows a background service programming front desk so low, is not likely to be recovered out priorities change services. It can be used to make the process keep alive. The current process allows to hold such a service, when the current process into the background due to hold front desk, will make the low-priority change the current process. Under normal circumstances, if after the current process does not hold a foreground process, the current process into the background priority is 5, if the holders of a foreground process, the priority becomes 2. So that is not easily recovered

Guess you like

Origin blog.csdn.net/reuxfhc/article/details/81712922