我的Android应用程序启动本地前台服务(与我的活动在同一个进程中)。即使系统因内存不足而破坏活动,该服务也必须保持活跃状态。
下次用户启动应用时,我需要检查服务是否正在运行。
我试图在服务类的public static boolean Started = false;
标志中存储状态(我知道这是不好的做法)。
我的活动在服务运行的过程中开始。我想,静态构造函数叫做一个。所以,我可以在活动中检查这个标志。但是,静态数据会重新初始化为默认值。
有人可以给我一个关于这种行为的暗示吗?
答案 0 :(得分:0)
这里只是一个示例,当应用程序出现forground时我如何重新启动服务
public void storeLastLocation() {
if (!isStoreLocationActive){
Log.d("IQ MA", "storeLastLocation() called");
String access_token = Configs.access_token;
Intent intent = new Intent(MainActivity.this, StoreLocationService.class);
intent.putExtra("access_token", access_token);
stopService(intent);
startService(intent);
isStoreLocationActive = true;
}
}
然后在服务上存储布尔值