Android 可以在应用程序处于前台时停止服务吗?
Can Android stop a service while app is in foreground?
在我的应用程序中,第一个 activity 的 onCreate 中启动了一项服务,我想确保该服务 运行 只要该应用程序保持在前台。
是否有可能在应用程序在前台时系统停止服务?
根据文档,有时可能是:
A service process is one holding a Service that has been started with the startService() method. Though these processes are not directly visible to the user, they are generally doing things that the user cares about (such as background network data upload or download), so the system will always keep such processes running unless there is not enough memory to retain all foreground and visible processes.
Services that have been running for a long time (such as 30 minutes or more) may be demoted in importance to allow their process to drop to the cached LRU list described next. This helps avoid situations where very long running services with memory leaks or other problems consume so much RAM that they prevent the system from making effective use of cached processes.
(source)
此处的最佳做法是使用 ForegroundService。它是一个常规服务,调用 startForeground() 方法确保服务 运行。例如,它被音乐播放器广泛使用......所以你可以想象,没有人喜欢系统在播放歌曲的过程中终止音乐服务。应该这样做。
Service Overview
中的更多内容
我不认为系统会在这种情况下单方面破坏你的Service
。也就是说,我从未见过它这样做(但我没有任何参考资料来支持它)。
如果您说“...并且服务绑定到 Activity”,我会更进一步:"The Service is not going to be destroyed while it is bound," 因为这意味着突然调用 onServiceDisconnected()
,而不是由 unbindService()
.
提示
首先你需要知道什么时候启动和停止服务。
创建 AppLifeCycleHandler Class
public class AppLifeCycleHandler implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {
private AppLifeCycleCallback appLifeCycleCallback;
private boolean appInForeground;
public AppLifeCycleHandler(AppLifeCycleCallback appLifeCycleCallback) {
this.appLifeCycleCallback = appLifeCycleCallback;
}
@Override
public void onActivityResumed(Activity activity) {
if (!appInForeground) {
appInForeground = true;
appLifeCycleCallback.onAppForeground();
}
}
@Override
public void onTrimMemory(int i) {
if (i == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
appInForeground = false;
appLifeCycleCallback.onAppBackground();
}
}
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
@Override
public void onConfigurationChanged(Configuration configuration) {
}
@Override
public void onLowMemory() {
}
interface AppLifeCycleCallback {
void onAppBackground();
void onAppForeground();
}
}
现在在你的public class MyApplication extends Application
里面
添加以下代码implements AppLifeCycleHandler.AppLifeCycleCallback
@Override
public void onCreate() {
super.onCreate();
AppLifeCycleHandler appLifeCycleHandler = new AppLifeCycleHandler(this);
registerActivityLifecycleCallbacks(appLifeCycleHandler);
registerComponentCallbacks(appLifeCycleHandler);
}
最后:
@Override
public void onAppBackground() {
Log.d("LifecycleEvent", "onAppBackground");
}
@Override
public void onAppForeground() {
Log.d("LifecycleEvent", "onAppForeground");
}
现在你需要做的最后一件事就是创建服务,按照这个 link
create-start-stop-android-background-service
并将您的开始意图和停止意图放入我们刚刚在您的应用程序中创建的方法中 class
在我的应用程序中,第一个 activity 的 onCreate 中启动了一项服务,我想确保该服务 运行 只要该应用程序保持在前台。
是否有可能在应用程序在前台时系统停止服务?
根据文档,有时可能是:
A service process is one holding a Service that has been started with the startService() method. Though these processes are not directly visible to the user, they are generally doing things that the user cares about (such as background network data upload or download), so the system will always keep such processes running unless there is not enough memory to retain all foreground and visible processes. Services that have been running for a long time (such as 30 minutes or more) may be demoted in importance to allow their process to drop to the cached LRU list described next. This helps avoid situations where very long running services with memory leaks or other problems consume so much RAM that they prevent the system from making effective use of cached processes.
(source)
此处的最佳做法是使用 ForegroundService。它是一个常规服务,调用 startForeground() 方法确保服务 运行。例如,它被音乐播放器广泛使用......所以你可以想象,没有人喜欢系统在播放歌曲的过程中终止音乐服务。应该这样做。 Service Overview
中的更多内容我不认为系统会在这种情况下单方面破坏你的Service
。也就是说,我从未见过它这样做(但我没有任何参考资料来支持它)。
如果您说“...并且服务绑定到 Activity”,我会更进一步:"The Service is not going to be destroyed while it is bound," 因为这意味着突然调用 onServiceDisconnected()
,而不是由 unbindService()
.
首先你需要知道什么时候启动和停止服务。
创建 AppLifeCycleHandler Class
public class AppLifeCycleHandler implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {
private AppLifeCycleCallback appLifeCycleCallback;
private boolean appInForeground;
public AppLifeCycleHandler(AppLifeCycleCallback appLifeCycleCallback) {
this.appLifeCycleCallback = appLifeCycleCallback;
}
@Override
public void onActivityResumed(Activity activity) {
if (!appInForeground) {
appInForeground = true;
appLifeCycleCallback.onAppForeground();
}
}
@Override
public void onTrimMemory(int i) {
if (i == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
appInForeground = false;
appLifeCycleCallback.onAppBackground();
}
}
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
@Override
public void onConfigurationChanged(Configuration configuration) {
}
@Override
public void onLowMemory() {
}
interface AppLifeCycleCallback {
void onAppBackground();
void onAppForeground();
}
}
现在在你的public class MyApplication extends Application
添加以下代码implements AppLifeCycleHandler.AppLifeCycleCallback
@Override
public void onCreate() {
super.onCreate();
AppLifeCycleHandler appLifeCycleHandler = new AppLifeCycleHandler(this);
registerActivityLifecycleCallbacks(appLifeCycleHandler);
registerComponentCallbacks(appLifeCycleHandler);
}
最后:
@Override
public void onAppBackground() {
Log.d("LifecycleEvent", "onAppBackground");
}
@Override
public void onAppForeground() {
Log.d("LifecycleEvent", "onAppForeground");
}
现在你需要做的最后一件事就是创建服务,按照这个 link create-start-stop-android-background-service 并将您的开始意图和停止意图放入我们刚刚在您的应用程序中创建的方法中 class