我需要最好的解决方案。当我收到推送通知时,我想知道我的应用程序是否打开,因为如果我的应用程序打开时用户点击通知我想调用特定方法,但如果应用程序已关闭我想打开应用程序
答案 0 :(得分:0)
基本上您需要的是检查您的某个活动是否在Foreground中。您可以查看此How to determine if one of my activities is in the foreground
答案 1 :(得分:-1)
//Use this method to check your app is running or not
public boolean isAppRunning(){
String packageName="Your package name";
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningAppProcessInfo> procInfoslist = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfoslist.size(); i++)
{
if(procInfoslist.get(i).processName.equals(packageName))
{
return true;
}
}
return false;
}
`
//Use this method to check any activity is running or not
public boolean isActivityRunning(Context ctx) {
ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo task : tasks) {
if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
return true;
}
return false;
}