當前位置: 首頁>>代碼示例>>Java>>正文


Java ActivityManager.getRunningAppProcesses方法代碼示例

本文整理匯總了Java中android.app.ActivityManager.getRunningAppProcesses方法的典型用法代碼示例。如果您正苦於以下問題:Java ActivityManager.getRunningAppProcesses方法的具體用法?Java ActivityManager.getRunningAppProcesses怎麽用?Java ActivityManager.getRunningAppProcesses使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.app.ActivityManager的用法示例。


在下文中一共展示了ActivityManager.getRunningAppProcesses方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: isBackground

import android.app.ActivityManager; //導入方法依賴的package包/類
public static boolean isBackground(Context context) {
    if (context == null) {
        return false;
    }

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (activityManager == null) {
        return true;
    }

    List<ActivityManager.RunningAppProcessInfo> appProcesses = null;
    appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null || appProcesses.size() == 0) {
        return false;
    }
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(context.getPackageName())) {
            return appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
        }
    }
    return false;
}
 
開發者ID:pre-dem,項目名稱:pre-dem-android,代碼行數:23,代碼來源:Functions.java

示例2: isMainProcess

import android.app.ActivityManager; //導入方法依賴的package包/類
/**
 * is main process
 *
 * @param context
 * @return
 */
public static boolean isMainProcess(Context context) {
    try {
        ActivityManager am = ((ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfo = am.getRunningAppProcesses();
        String mainProcessName = context.getPackageName();
        int myPid = android.os.Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : processInfo) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
 
開發者ID:liuzhao2007,項目名稱:StartUp-Protector,代碼行數:24,代碼來源:ProtectorUtils.java

示例3: isNamedProcess

import android.app.ActivityManager; //導入方法依賴的package包/類
/**
 * whether this process is named with processName
 *
 * @param context     上下文
 * @param processName 進程名
 * @return 是否含有當前的進程
 */
public static boolean isNamedProcess(Context context, String processName) {
    if (context == null || TextUtils.isEmpty(processName)) {
        return false;
    }

    int pid = android.os.Process.myPid();
    ActivityManager manager = (ActivityManager) context.getSystemService(
            Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processInfoList
            = manager.getRunningAppProcesses();
    if (processInfoList == null) {
        return true;
    }

    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
        if (processInfo.pid == pid &&
                processName.equalsIgnoreCase(processInfo.processName)) {
            return true;
        }
    }
    return false;
}
 
開發者ID:weileng11,項目名稱:KUtils-master,代碼行數:30,代碼來源:Utils.java

示例4: killBackgroundProcesses

import android.app.ActivityManager; //導入方法依賴的package包/類
@Override
public boolean killBackgroundProcesses(String pluginPackageName) throws RemoteException {
    ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> infos = am.getRunningAppProcesses();
    boolean success = false;
    for (RunningAppProcessInfo info : infos) {
        if (info.pkgList != null) {
            String[] pkgListCopy = Arrays.copyOf(info.pkgList, info.pkgList.length);
            Arrays.sort(pkgListCopy);
            if (Arrays.binarySearch(pkgListCopy, pluginPackageName) >= 0 && info.pid != android.os.Process.myPid()) {
                Log.i(TAG, "killBackgroundProcesses(%s),pkgList=%s,pid=%s", pluginPackageName, Arrays.toString(info.pkgList), info.pid);
                android.os.Process.killProcess(info.pid);
                success = true;
            }
        }
    }
    return success;
}
 
開發者ID:amikey,項目名稱:DroidPlugin,代碼行數:19,代碼來源:IPluginManagerImpl.java

示例5: isAppAlive

import android.app.ActivityManager; //導入方法依賴的package包/類
/**
 * 判斷應用是否已經啟動
 *
 * @param context     一個context
 * @param packageName 要判斷應用的包名
 * @return boolean
 */
public static boolean isAppAlive(Context context, String packageName) {
    ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
    for (int i = 0; i < processInfos.size(); i++) {
        if (processInfos.get(i).processName.equals(packageName)) {
            Log.i("NotificationLaunch",
                    String.format("the %s is running, isAppAlive return true", packageName));
            return true;
        }
    }
    Log.i("NotificationLaunch",
            String.format("the %s is not running, isAppAlive return false", packageName));
    return false;
}
 
開發者ID:liuyongfeng90,項目名稱:JKCloud,代碼行數:23,代碼來源:SystemUtils.java

示例6: getMyProcessName

import android.app.ActivityManager; //導入方法依賴的package包/類
public static String getMyProcessName(Context context) {
    String processName = "";
    ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> infoList = am.getRunningAppProcesses();
    if (infoList == null) {
        throw new RuntimeException("getRunningAppProcesses() returns 'null'.");
    }
    for (RunningAppProcessInfo info : infoList) {
        try {
            if (info.pid == Process.myPid()) {
                processName = info.processName;
                break;
            }
        } catch (Exception ignored) {
        }
    }
    return processName;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:19,代碼來源:Utils.java

示例7: isAppIsInBackground

import android.app.ActivityManager; //導入方法依賴的package包/類
/**
 * Method check if app is in background or in foreground
 *
 * @param context this contentx
 * @return true if app is in background or false if app in foreground
 */

private 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<ActivityManager.RunningAppProcessInfo> 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<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(context.getPackageName())) {
            isInBackground = false;
        }
    }

    return isInBackground;
}
 
開發者ID:codingdojoangola,項目名稱:cda-app,代碼行數:32,代碼來源:FirebaseService.java

示例8: isAppAlive

import android.app.ActivityManager; //導入方法依賴的package包/類
public static boolean isAppAlive(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcessInfo :
                appProcessInfos
                ) {
                if (appProcessInfo.processName.equals(context.getPackageName())) {
                        LogUtil.e("當前的APP存活");
                        return true;
                }
        }
        return false;
}
 
開發者ID:HelloChenJinJun,項目名稱:TestChat,代碼行數:14,代碼來源:CommonUtils.java

示例9: isAppAlive

import android.app.ActivityManager; //導入方法依賴的package包/類
public static boolean isAppAlive(Context context, String packageName){
    ActivityManager activityManager =
            (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processInfos
            = activityManager.getRunningAppProcesses();
    for(int i = 0; i < processInfos.size(); i++){
        if(processInfos.get(i).processName.equals(packageName)){
            return true;
        }
    }
    return false;
}
 
開發者ID:ThirtyDegreesRay,項目名稱:OpenHub,代碼行數:13,代碼來源:AppOpener.java

示例10: isCallerForegroundOrSelf

import android.app.ActivityManager; //導入方法依賴的package包/類
/**
 * @return true when inside a Binder transaction and the caller is in the
 * foreground or self. Don't use outside a Binder transaction.
 */
private boolean isCallerForegroundOrSelf() {
    int uid = Binder.getCallingUid();
    if (uid == Process.myUid()) return true;
    // Starting with L MR1, AM.getRunningAppProcesses doesn't return all the
    // processes. We use a workaround in this case.
    boolean useWorkaround = true;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
        do {
            ActivityManager am =
                    (ActivityManager) mApplication.getSystemService(Context.ACTIVITY_SERVICE);
            // Extra paranoia here and below, some L 5.0.x devices seem to throw NPE somewhere
            // in this code.
            // See https://crbug.com/654705.
            if (am == null) break;
            List<ActivityManager.RunningAppProcessInfo> running = am.getRunningAppProcesses();
            if (running == null) break;
            for (ActivityManager.RunningAppProcessInfo rpi : running) {
                if (rpi == null) continue;
                boolean matchingUid = rpi.uid == uid;
                boolean isForeground = rpi.importance
                        == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
                useWorkaround &= !matchingUid;
                if (matchingUid && isForeground) return true;
            }
        } while (false);
    }
    return useWorkaround ? !isBackgroundProcess(Binder.getCallingPid()) : false;
}
 
開發者ID:rkshuai,項目名稱:chromium-for-android-56-debug-video,代碼行數:33,代碼來源:CustomTabsConnection.java

示例11: isAppForeground

import android.app.ActivityManager; //導入方法依賴的package包/類
public static boolean isAppForeground() {
    ActivityManager activityManager = (ActivityManager) mApplication.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
        return false;
    }
    final String packageName = mApplication.getPackageName();
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(packageName)
                && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            return true;
        }
    }
    return false;
}
 
開發者ID:TeamThresh,項目名稱:Lantern-sdk,代碼行數:16,代碼來源:RYLA.java

示例12: getCurProcessName

import android.app.ActivityManager; //導入方法依賴的package包/類
public static String getCurProcessName(Context context) {
    int pid = android.os.Process.myPid();
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
        if (appProcess.pid == pid) {
            return appProcess.processName;
        }
    }
    return null;
}
 
開發者ID:zqHero,項目名稱:rongyunDemo,代碼行數:11,代碼來源:App.java

示例13: getRemoteProcessInfo

import android.app.ActivityManager; //導入方法依賴的package包/類
private ActivityManager.RunningAppProcessInfo getRemoteProcessInfo() {
    ActivityManager manager = (ActivityManager)getContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processInfoList = manager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo info : processInfoList) {
        if (info.processName.equals(getContext().getPackageName() + REMOTE_PROCESS_POSTFIX)) {
            return info;
        }
    }

    return null;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:12,代碼來源:RunWithRemoteService.java

示例14: shouldInit

import android.app.ActivityManager; //導入方法依賴的package包/類
private boolean shouldInit() {
    ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
    List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
    String mainProcessName = getPackageName();
    int myPid = Process.myPid();
    for (ActivityManager.RunningAppProcessInfo info : processInfos) {
        if (info.pid == myPid && mainProcessName.equals(info.processName)) {
            return true;
        }
    }
    return false;
}
 
開發者ID:liuyongfeng90,項目名稱:JKCloud,代碼行數:13,代碼來源:MyApplication.java

示例15: getProcessName

import android.app.ActivityManager; //導入方法依賴的package包/類
/**
 * 獲取當前進程名
 * @param context
 * @return 進程名
 */
public static final String getProcessName(Context context) {
    String processName = null;

    // ActivityManager
    ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE));

    while (true) {
        for (ActivityManager.RunningAppProcessInfo info : am.getRunningAppProcesses()) {
            if (info.pid == android.os.Process.myPid()) {
                processName = info.processName;

                break;
            }
        }

        // go home
        if (!TextUtils.isEmpty(processName)) {
            return processName;
        }

        // take a rest and again
        try {
            Thread.sleep(100L);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
 
開發者ID:newDeepLearing,項目名稱:decoy,代碼行數:34,代碼來源:SystemUtil.java


注:本文中的android.app.ActivityManager.getRunningAppProcesses方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。