如何检测应用程序是否关闭
How to detected if application is closed
我有一个应用程序从 Broadcastreceiver 接收数据以从网络中获取新数据 (json),当我的应用程序在 运行 获取数据时;一切运行良好,当应用程序关闭时它崩溃 "Your app stopped working"。即使是以下检查我的应用程序是否在前台的好方法也不起作用。
我想要它,所以如果应用程序关闭,而不是 运行,调用一个打开应用程序的意图,使其不会崩溃,那么如何检查应用程序是否关闭?
我的代码;
public class UpdateReceiver extends BroadcastReceiver {
private static long alarmTime = 0;
@Override
public void onReceive(Context context, Intent intent) {
if (isAppForground(context)){
Toast.makeText(context, "APP IN FOREGROUND", Toast.LENGTH_LONG).show();
} else {
Intent intentActivity = new Intent(context, MainActivity.class);
intentActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentActivity);
Toast.makeText(context, "APP NOT RUNNING", Toast.LENGTH_LONG).show();
}
//set new alarm for next tuesday
UpcomingFragment.getInstance().setAlarm(-1);
//updates the current json
UpcomingFragment.getInstance().update();
}
public static long getAlermTimeInMillis(){
return alarmTime;
}
public boolean isAppForground(Context mContext) {
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(mContext.getPackageName())) {
return false;
}
}
return true;
}
}
我有完全相同的问题,我被困了天。幸运的是,在第 3 天,我找到了一个可行的解决方案。
public void onTrimMemory(final int level) {
if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
//SCREEN IS NOT SHOWING
}
这个onTrimMemory
方法非常有用。它应该用于 优化 您的内存中的应用程序,但我们也可以像这样利用它来发挥我们的优势!
我有一个应用程序从 Broadcastreceiver 接收数据以从网络中获取新数据 (json),当我的应用程序在 运行 获取数据时;一切运行良好,当应用程序关闭时它崩溃 "Your app stopped working"。即使是以下检查我的应用程序是否在前台的好方法也不起作用。 我想要它,所以如果应用程序关闭,而不是 运行,调用一个打开应用程序的意图,使其不会崩溃,那么如何检查应用程序是否关闭?
我的代码;
public class UpdateReceiver extends BroadcastReceiver {
private static long alarmTime = 0;
@Override
public void onReceive(Context context, Intent intent) {
if (isAppForground(context)){
Toast.makeText(context, "APP IN FOREGROUND", Toast.LENGTH_LONG).show();
} else {
Intent intentActivity = new Intent(context, MainActivity.class);
intentActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentActivity);
Toast.makeText(context, "APP NOT RUNNING", Toast.LENGTH_LONG).show();
}
//set new alarm for next tuesday
UpcomingFragment.getInstance().setAlarm(-1);
//updates the current json
UpcomingFragment.getInstance().update();
}
public static long getAlermTimeInMillis(){
return alarmTime;
}
public boolean isAppForground(Context mContext) {
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(mContext.getPackageName())) {
return false;
}
}
return true;
}
}
我有完全相同的问题,我被困了天。幸运的是,在第 3 天,我找到了一个可行的解决方案。
public void onTrimMemory(final int level) {
if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
//SCREEN IS NOT SHOWING
}
这个onTrimMemory
方法非常有用。它应该用于 优化 您的内存中的应用程序,但我们也可以像这样利用它来发挥我们的优势!