Java Code Examples for android.app.ActivityManager#getRunningTasks()

The following examples show how to use android.app.ActivityManager#getRunningTasks() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppUtil.java    From FamilyChat with Apache License 2.0 7 votes vote down vote up
/**
 * 判断某个界面是否在前台
 *
 * @param context
 * @param className 某个界面名称
 */
public static boolean isForeground(Context context, String className)
{
    if (context == null || TextUtils.isEmpty(className))
    {
        return false;
    }

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
    if (list != null && list.size() > 0)
    {
        ComponentName cpn = list.get(0).topActivity;
        if (className.equals(cpn.getClassName()))
        {
            return true;
        }
    }
    return false;
}
 
Example 2
Source File: SystemUtils.java    From shinny-futures-android with GNU General Public License v3.0 7 votes vote down vote up
/**
 * 将本应用置顶到最前端
 * 当本应用位于后台时,则将它切换到最前端
 *
 * @param context
 */
public static void setTopApp(Context context) {
    /**获取ActivityManager*/
    ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        List<ActivityManager.AppTask> list = activityManager.getAppTasks();
        for (ActivityManager.AppTask appTask : list){
            appTask.moveToFront();
            break;
        }
    }else {
        /**获得当前运行的task(任务)*/
        List<ActivityManager.RunningTaskInfo> taskInfoList = activityManager.getRunningTasks(100);
        for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
            /**找到本应用的 task,并将它切换到前台*/
            if (taskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
                activityManager.moveTaskToFront(taskInfo.id, 0);
                break;
            }
        }
    }
}
 
Example 3
Source File: PackageManagerUtil.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/**
 * 只能获取APP范围内的
 * @param context
 * @return
 */
public static ComponentName[] getAppTopAndBaseActivitys(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    if (activityManager != null) {
        List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
        if (runningTaskInfos != null && runningTaskInfos.size() > 0) {
            RunningTaskInfo topRunningTask = runningTaskInfos.get(0);
            if (topRunningTask != null) {
                if (topRunningTask.numActivities > 0) {
                    CommonLog.e(TAG, "---> getAppTopAndBaseActivitys() top:" + topRunningTask.topActivity/*.getPackageName()*/
                            + "  base:" + topRunningTask.baseActivity
                    );
                    ComponentName[] topAndBaseComponents = {
                            topRunningTask.topActivity,
                            topRunningTask.baseActivity
                    };
                    return topAndBaseComponents;
                }
            }
        }
    }
    return null;
}
 
Example 4
Source File: AppToolUtils.java    From YCAudioPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 判断app是否正在运行
 * @param context                       上下文
 * @param packageName                   应用的包名
 * @return true 表示正在运行,false 表示没有运行
 */
public static boolean isAppRunning(Context context, String packageName) {
    ActivityManager am = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> list ;
    if (am != null) {
        list = am.getRunningTasks(100);
        if (list.size() <= 0) {
            return false;
        }
        for (ActivityManager.RunningTaskInfo info : list) {
            if (info.baseActivity.getPackageName().equals(packageName)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 5
Source File: PackageUtil.java    From AndroidBase with Apache License 2.0 6 votes vote down vote up
/**
 * 获取指定程序信息
 */
public static ActivityManager.RunningTaskInfo getTopRunningTask(Context context) {
    try {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        // 得到当前正在运行的任务栈
        List<ActivityManager.RunningTaskInfo> runningTasks = am.getRunningTasks(1);
        // 得到前台显示的任务栈
        ActivityManager.RunningTaskInfo runningTaskInfo = runningTasks.get(0);
        return runningTaskInfo;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 6
Source File: AppInfoUtil.java    From BaseProject with MIT License 6 votes vote down vote up
/**
 * 判断当前应用程序是否处于后台
 * <pre>需要权限:&lt;uses-permission android:name="android.permission.GET_TASKS" /&gt;  </pre>
 */
@SuppressWarnings("deprecation")
@Deprecated
public static boolean isApplicationToBackground(@NonNull Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (null == am) {
        return false;
    }
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}
 
Example 7
Source File: NotificationUtils.java    From LNMOnlineAndroidSample with Apache License 2.0 6 votes vote down vote up
/**
 * Method checks if the app is in background or not
 */
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<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;
}
 
Example 8
Source File: SystemUtils.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
/**
 * 判断是否在当前Activity是否在栈顶
 */
public static boolean isThisActivity(Context context, String className) {
    if (context == null || TextUtils.isEmpty(className)) {
        return false;
    }

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
    if (list != null && list.size() > 0) {
        ComponentName cpn = list.get(0).topActivity;
        if (className.equals(cpn.getClassName())) {
            return true;
        }
    }

    return false;
}
 
Example 9
Source File: DialogService.java    From letv with Apache License 2.0 5 votes vote down vote up
public boolean isHome() {
    ActivityManager mActivityManager = (ActivityManager) getSystemService("activity");
    List<String> homeList = getHomes();
    List<RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);
    if (homeList != null) {
        return homeList.contains(((RunningTaskInfo) rti.get(0)).topActivity.getPackageName());
    }
    return false;
}
 
Example 10
Source File: CommonUtils.java    From SmartChart with Apache License 2.0 5 votes vote down vote up
public static boolean isSingleActivity(Context var0) {
    ActivityManager var1 = (ActivityManager) var0.getSystemService("activity_message");
    List var2 = null;

    try {
        var2 = var1.getRunningTasks(1);
    } catch (SecurityException var4) {
        var4.printStackTrace();
    }

    return var2 != null && var2.size() >= 1 ? ((ActivityManager.RunningTaskInfo) var2.get(0)).numRunning == 1 : false;
}
 
Example 11
Source File: AppUtils.java    From zone-sdk with MIT License 5 votes vote down vote up
/**
 * need < uses-permission android:name =“android.permission.GET_TASKS” />
 * 判断是否前台运行
 */
public static boolean isRunningForeground(Context context) {
	ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
	List<ActivityManager.RunningTaskInfo> taskList = am.getRunningTasks(1);
	if (taskList != null && !taskList.isEmpty()) {
		ComponentName componentName = taskList.get(0).topActivity;
		if (componentName != null && componentName.getPackageName().equals(context.getPackageName())) {
			return true;
		}
	}
	return false;
}
 
Example 12
Source File: Kits.java    From XDroidMvp with MIT License 5 votes vote down vote up
/**
 * 判断当前应用是否运行在后台
 *
 * @param context
 * @return
 */
public static boolean isApplicationInBackground(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskList = am.getRunningTasks(1);
    if (taskList != null && !taskList.isEmpty()) {
        ComponentName topActivity = taskList.get(0).topActivity;
        if (topActivity != null && !topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}
 
Example 13
Source File: ActivityHelper.java    From RedEnvelopeAssistant with MIT License 5 votes vote down vote up
/**
 * 慎重使用此函数  be cautious to use this<br/>
 * GET_TASK was deprecated in API level 21. No longer enforced
 */
public static String getTopActivityName(Context context) {
	try {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
		if (!tasks.isEmpty()) {
			ComponentName topActivity = tasks.get(0).topActivity;
			return topActivity.getClassName();
		}
	}catch (Exception ex){
		ex.printStackTrace();
	}
	return "";
}
 
Example 14
Source File: CurrentActivity.java    From supermonkey with Apache License 2.0 5 votes vote down vote up
@Override
public Command.Result call() throws Exception {

	ActivityManager am = (ActivityManager) applicationContext.getSystemService(Context.ACTIVITY_SERVICE);

	List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);

	for (ActivityManager.RunningTaskInfo task : tasks) {
		ComponentName topActivity = task.topActivity;
		String response = "{ \"package\":\"" + topActivity.getPackageName() + "\", \"class\":\"" + topActivity.getClassName() + "\" }";
		return Command.Result.success(toJson(response));
	}

	return Command.Result.failure("No tasks running");
}
 
Example 15
Source File: AppTaskUtil.java    From JD-Test with Apache License 2.0 5 votes vote down vote up
/**
 *判断当前应用程序处于前台还是后台
 */
public static boolean isApplicationBroughtToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}
 
Example 16
Source File: ActivityStackCompat.java    From Recovery with Apache License 2.0 5 votes vote down vote up
private static ActivityManager.RunningTaskInfo getTopTaskBeforeL(Context context) {
    ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = null;
    try {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
            tasks = activityManager.getRunningTasks(1);
    } catch (Exception e) {
        return null;
    }
    if (tasks == null || tasks.size() == 0)
        return null;
    return tasks.get(0);
}
 
Example 17
Source File: APPUtil.java    From DownloadManager with Apache License 2.0 5 votes vote down vote up
/**
 * 判断当前APP处于前台还是后台;
 * 需添加<uses-permission android:name="android.permission.GET_TASKS"/>
 * 并且必须是系统应用,该方法才有效
 * @param context
 * @return
 */
public static boolean isApplicationBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    @SuppressWarnings("deprecation")
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (tasks != null) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}
 
Example 18
Source File: AppUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 5 votes vote down vote up
/**
 * whether application is in background
 * <ul>
 * <li>need use permission android.permission.GET_TASKS in Manifest.xml</li>
 * </ul>
 * @param context 上下文
 * @return if application is in background return true, otherwise return
 * false
 */
public static boolean isApplicationInBackground(Context context) {
    ActivityManager am = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskList = am.getRunningTasks(1);
    if (taskList != null && !taskList.isEmpty()) {
        ComponentName topActivity = taskList.get(0).topActivity;
        if (topActivity != null
                && !topActivity.getPackageName().equals(
                context.getPackageName())) {
            return true;
        }
    }
    return false;
}
 
Example 19
Source File: EaseCommonUtils.java    From nono-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取栈顶的activity
 * @param context
 * @return
 */
public static String getTopActivity(Context context) {
	ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
	List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

	if (runningTaskInfos != null)
		return runningTaskInfos.get(0).topActivity.getClassName();
	else
		return "";
}
 
Example 20
Source File: XAppUtils.java    From XFrame with Apache License 2.0 3 votes vote down vote up
/**
 need < uses-permission android:name ="android.permission.GET_TASKS"/>

 判断是否前台运行

 之前,使用该接口需要 android.permission.GET_TASKS
 即使是自己开发的普通应用,只要声明该权限,即可以使用getRunningTasks接口。
 但从L开始,这种方式以及废弃。
 应用要使用该接口必须声明权限android.permission.REAL_GET_TASKS
 而这个权限是不对三方应用开放的。(在Manifest里申请了也没有作用)
 系统应用(有系统签名)可以调用该权限。
 */
public static boolean isRunningForeground() {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskList = am.getRunningTasks(1);
    if (taskList != null && !taskList.isEmpty()) {
        ComponentName componentName = taskList.get(0).topActivity;
        if (componentName != null && componentName.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}