【发布时间】:2019-09-12 14:46:38
【问题描述】:
我正在使用 MVVM 模式开发小型 android 应用程序。
我的问题是我的 ViewModel MyActivity 中的观察者没有从后台调用。即使应用程序在后台,我也需要调用它以向用户显示系统 Notification 应用程序计算过程已完成并且结果已准备好。
这是位于onCreate in MyActivity 中的当前实现:
mainActivityViewModel.getTestResult().observe(MainActivity.this, new Observer<String>() {
@Override
public void onChanged(@Nullable String blogList) {
Toast.makeText(getApplicationContext(), "test...", Toast.LENGTH_SHORT).show();
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)){
//The app is in foreground - showDialog
}else{
//The app is in background - showNotification
}
}
目前,仅当应用程序处于前台时才会调用此观察者 - 如果进程在应用程序处于前台时完成 - 'showDialog' 将触发,如果应用程序处于后台 - showNotification 将触发 - 但仅在之后我会再次打开应用程序。这不是我试图实现的行为。请帮忙!谢谢。
【问题讨论】:
-
你不能这就是android的工作方式......但你可以尝试为此使用前台服务
-
嗨@Selvin,您能详细说明一下吗?什么样的服务?谢谢。
标签: android android-mvvm