【发布时间】:2015-11-20 03:19:07
【问题描述】:
我有一个应用程序从 Broadcastreceiver 接收以从 web(json) 获取新数据,当我的应用程序在获取时运行;一切运行良好,当应用程序关闭时它崩溃“你的应用程序停止工作”。即使是以下检查我的应用程序是否在前台的好方法也不起作用。 我想要它,所以如果应用程序已关闭,未运行,则调用意图打开应用程序以使其不会崩溃,那么如何检查应用程序是否已关闭?
我的代码;
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;
}
}
【问题讨论】:
-
从设备添加崩溃日志
-
也许我的 Android 工作室有时出了问题?
-
使用此命令将其保存在文件 adb logcat > crashlogs.txt
-
我在哪里执行这个命令?
标签: android android-intent broadcastreceiver android-broadcast