融云im 推送显示的问题

先说说融云im的坑吧 。

融云聊天唤起推送的时候是用isInBackground这个方法 ,这个方法在

package io.rong.common;包下 
SystemUtils类中

融云im判断是否在后台用的是

public static boolean isInBackground(Context context) {
    boolean isInBackground = true;
    ActivityManager am = (ActivityManager)context.getSystemService("activity");
    List runningProcesses;
    if(Build.VERSION.SDK_INT > 20) {
        runningProcesses = am.getRunningAppProcesses();
        if(runningProcesses == null) {
            Log.e("runningProcesses","null");
            return true;
        }

        Iterator var4 = runningProcesses.iterator();

        while(true) {
            ActivityManager.RunningAppProcessInfo processInfo;
            do {
                if(!var4.hasNext()) {
                    Log.e("var4.hasNext()",isInBackground+"");
                    return isInBackground;
                }

                processInfo = (ActivityManager.RunningAppProcessInfo)var4.next();
            } while(processInfo.importance != 100);

            String[] var6 = processInfo.pkgList;
            int var7 = var6.length;

            for(int var8 = 0; var8 < var7; ++var8) {
                String activeProcess = var6[var8];
                if(activeProcess.equals(context.getPackageName())) {
                    Log.e("SystemUtils", "the process is in foreground:" + processInfo+"???");
                    Log.e("循环",false+"");
                    return false;
                }
            }
        }
    } else {
        runningProcesses = am.getRunningTasks(1);
        ComponentName componentInfo = ((ActivityManager.RunningTaskInfo)runningProcesses.get(0)).topActivity;
        if(componentInfo.getPackageName().equals(context.getPackageName())) {
            isInBackground = false;
        }
    }
    Log.e("true3",isInBackground+"");
    return isInBackground;
}

 

代码细化 :

public static boolean isAppIsInBackground(Context context) {
    boolean isInBackground = true;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
        List runningProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
            //前台程序
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String activeProcess : processInfo.pkgList) {
                    if (activeProcess.equals(context.getPackageName())) {
                        isInBackground = false;
                    }
                }
            }
        }
    } else {
        List taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(context.getPackageName())) {
            isInBackground = false;
        }
    }

    return isInBackground;
}

用这种方式判断是否在前后台是有问题的。

参考地址 :https://www.jianshu.com/p/82142ccd7c04

用runningProcesses 这个东西 如果有service 常驻后台是会一直返回true的 。不信自己调试!!!!

因为我们项目用到了极光推送

也就是pushservice这个服务会影响到runningProcesses。

幸运的是因为这个服务是支持多进程的 。

android:process=":xxx"

给定他一个进程。就完美解决了。

 

 

你可能感兴趣的:(融云im 推送显示的问题)