ActivityManager.getRunningTasks is deprecated android

前端 未结 5 1275
情书的邮戳
情书的邮戳 2020-12-01 05:51

I am working on push notification in android where i am using a below method to show notification, but the problem is that now ActivityManager.getRunningTasks(1); is being

5条回答
  •  遥遥无期
    2020-12-01 06:17

    May you want this:

    /***
     * Checking Whether any Activity of Application is running or not
     * @param context
     * @return
     */
    public static boolean isForeground(Context context) {
    
        // Get the Activity Manager
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    
        // Get a list of running tasks, we are only interested in the last one,
        // the top most so we give a 1 as parameter so we only get the topmost.
        List task = manager.getRunningAppProcesses();
    
        // Get the info we need for comparison.
        ComponentName componentInfo = task.get(0).importanceReasonComponent;
    
        // Check if it matches our package name.
        if(componentInfo.getPackageName().equals(context.getPackageName()))
            return true;
    
        // If not then our app is not on the foreground.
        return false;
    }
    

提交回复
热议问题