如何在应用关闭时显示通知,并在通知管理器中打开应用时隐藏

时间:2015-01-27 04:28:18

标签: android

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

    }

    public static boolean isActivityVisible() {
        return activityVisible;
    }

    public static void activityResumed() {
        activityVisible = true;

    }

    public static void activityPaused() {
        activityVisible = false;
    }

    private static boolean activityVisible;
    public static String FID = "";

}

应用程序的条件是打开的,然后不显示通知,如果应用程序是关闭显示通知。

if(LociiApplication.isActivityVisible())
                            System.out.println("Dont show notifications!");
                        else
                            sendNotification(msg, senderID);
                            Log.i(TAG, "Received: " + extras.toString());

实际上我想应用一个条件,当应用程序打开时,当应用程序关闭时,通知管理器中将显示没有通知,则应显示通知。我试图像这样实现,但它不起作用

1 个答案:

答案 0 :(得分:0)

如果您的应用位于前台,则以下代码将返回true,否则将返回false

 public boolean isAppInForeground(Context mCtx) {
        ActivityManager activityManager = (ActivityManager) mCtx.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> services = activityManager
                .getRunningTasks(Integer.MAX_VALUE);
        boolean isAppInForeGround = false;

        if (services.get(0).topActivity.getPackageName().toString()
                .equalsIgnoreCase(mCtx.getPackageName().toString())) {
            isAppInForeGround = true;
        }

      return isAppInForeGround;
    }