这是方案......
如何从运行服务中确定该应用程序是否已被杀死?这可能吗?
答案 0 :(得分:1)
我认为这就是你要找的东西:
private boolean isAppRunning() {
ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningTaskInfo task : manager.getRunningTasks(Integer.MAX_VALUE)) {
if ("com.example.YourLauncherActivityClassFullName".equals(task.baseActivity.getClassName())) {
return true;
}
}
return false;
}
您唯一需要提供的是应用的完整启动器活动类名称。
答案 1 :(得分:0)
要检查您的活动是否正在运行,您应该使用
ArrayList<String> runningactivities = new ArrayList<String>();
ActivityManager activityManager = (ActivityManager)getBaseContext().getSystemService (Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i1 = 0; i1 < services.size(); i1++) {
runningactivities.add(0,services.get(i1).topActivity.toString());
}
if(runningactivities.contains("ComponentInfo{com.app/com.app.main.MyActivity}")==true){
Toast.makeText(getBaseContext(),"Activity is in foreground, active",1000).show();
}
答案 2 :(得分:0)
在你的主要(第一)活动中:
public static boolean isRunning;
@覆盖
public void onCreate(Bundle savedInstanceState) {
isRunning = true;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
}
@Override
public void finish() {
isRunning = false;
super.finish();
}
检查服务中的isRunning标志。