简体   繁体   English

Android-收到推送通知后如何检查我的应用程序是否打开?

[英]Android - How to check my app is open when receive a push-notification?

I need best solution. 我需要最好的解决方案。 When I received a push notification, I want to know if my app is open or not, because if my application is open when the user onClick the notification I want to call specific method, but if the application is close I want to open the application. 当我收到推送通知时,我想知道我的应用程序是否打开,因为如果用户on时我的应用程序处于打开状态,请单击该通知,我想调用特定的方法,但是如果应用程序处于关闭状态,我想打开该应用程序。

Basically what you need is way to check if one of your activity is in Foreground. 基本上,您需要的是检查您的活动之一是否在前景中的方法。 May be you should check this How to determine if one of my activities is in the foreground 也许您应该检查一下如何确定我的活动之一是否在前台

//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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM