Java Code Examples for android.app.activitymanager#RunningTaskInfo

The following examples show how to use android.app.activitymanager#RunningTaskInfo . 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: 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 2
Source File: ActivityUtil.java    From star-zone-android with Apache License 2.0 6 votes vote down vote up
/**
 * 判断某个Activity 界面是否在前台
 * @param context
 * @param className 某个界面名称
 * @return
 */
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 = null;
    if (am != null) {
        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 3
Source File: WatchfulService.java    From Hangar with GNU General Public License v3.0 6 votes vote down vote up
protected void buildBaseTasks() {
    // taskList is blank!  Populating db from apps in memory.
    final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final List<ActivityManager.RunningTaskInfo> recentTasks = activityManager.getRunningTasks(MAX_RUNNING_TASKS);
    if (recentTasks != null && recentTasks.size() > 0) {
        for (ActivityManager.RunningTaskInfo recentTask : recentTasks) {
            ComponentName task = recentTask.baseActivity;
            try {
                String taskClass = task.getClassName();
                String taskPackage = task.getPackageName();

                buildTaskInfo(taskClass, taskPackage);
            } catch (NullPointerException e) {
            }
        }
    }
}
 
Example 4
Source File: SystemUtils.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
public static boolean isExitMainActivity(Context context, Class cls) {
    Intent intent = new Intent(context, cls);
    ComponentName cmpName = intent.resolveActivity(context.getPackageManager());
    boolean flag = false;
    if (cmpName != null) { // 说明系统中存在这个activity
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> taskInfoList = am.getRunningTasks(10);
        for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
            if (taskInfo.baseActivity.equals(cmpName)) { // 说明它已经启动了
                flag = true;
                break;  //跳出循环,优化效率
            }
        }
    }
    return flag;
}
 
Example 5
Source File: WorkUtil.java    From iMoney with Apache License 2.0 6 votes vote down vote up
/**
 * 判断应用是否在运行
 *
 * @param context
 * @return
 */
public static boolean isRun(Context context) {
    if (context == null) return false;
    Context appContext = context.getApplicationContext();
    ActivityManager am = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
    boolean isAppRunning = false;
    String MY_PKG_NAME = context.getPackageName();
    //100表示取的最大的任务数,info.topActivity表示当前正在运行的Activity,info.baseActivity表系统后台有此进程在运行
    for (ActivityManager.RunningTaskInfo info : list) {
        if (info.topActivity.getPackageName().equals(MY_PKG_NAME) || info.baseActivity.getPackageName().equals(MY_PKG_NAME)) {
            isAppRunning = true;
            Log.i(TAG, info.topActivity.getPackageName() + " info.baseActivity.getPackageName()=" + info.baseActivity.getPackageName());
            break;
        }
    }

    Log.i(TAG, "程序 isAppRunning......" + isAppRunning);
    return isAppRunning;
}
 
Example 6
Source File: RunningTasksJellyBean.java    From AcDisplay with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@SuppressWarnings("deprecation")
@Nullable
public String getRunningTasksTop(@NonNull Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
    return tasks == null || tasks.isEmpty() ? null : tasks.get(0).topActivity.getPackageName();
}
 
Example 7
Source File: SystemUtils.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
public static String getNameActivityOnTopStack() {
    ActivityManager activityManager = (ActivityManager) App.getInstance().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
    String ourPackageName = App.getInstance().getPackageName();
    String topActivityName = null;

    if (runningTasks != null) {
        for (ActivityManager.RunningTaskInfo taskInfo : runningTasks) {
            if (taskInfo.topActivity.getPackageName().equalsIgnoreCase(ourPackageName)) {
                topActivityName = taskInfo.topActivity.getClassName();
            }
        }
    }
    return topActivityName;
}
 
Example 8
Source File: AppCompatDlalog.java    From stynico with MIT License 5 votes vote down vote up
/**
    * 将当前应用运行到前台
    */
   private void bring2Front()
   {
       ActivityManager activtyManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
       List<ActivityManager.RunningTaskInfo> runningTaskInfos = activtyManager.getRunningTasks(3);
       for (ActivityManager.RunningTaskInfo runningTaskInfo : runningTaskInfos)
{
           if (this.getPackageName().equals(runningTaskInfo.topActivity.getPackageName()))
    {
               activtyManager.moveTaskToFront(runningTaskInfo.id, ActivityManager.MOVE_TASK_WITH_HOME);
               return;
           }
       }
   }
 
Example 9
Source File: AppUtil.java    From NewFastFrame with Apache License 2.0 5 votes vote down vote up
/**
 * 方法描述:判断某一应用是否正在运行
 * Created by cafeting on 2017/2/4.
 *
 * @param context     上下文
 * @param packageName 应用的包名
 * @return true 表示正在运行,false 表示没有运行
 */
public static boolean isAppRunning(Context context, String packageName) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> 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 10
Source File: RunningStatus.java    From pearl with Apache License 2.0 5 votes vote down vote up
public void run(){
    Looper.prepare();

    while(true){
        // Return a list of the tasks that are currently running,
        // with the most recent being first and older ones after in order.
        // Taken 1 inside getRunningTasks method means want to take only
        // top activity from stack and forgot the olders.
        List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
        String currentRunningActivityName = taskInfo.get(0).topActivity.getClassName();
        if (currentRunningActivityName.equals("PACKAGE_NAME.ACTIVITY_NAME")) {
            // show your activity here on top of PACKAGE_NAME.ACTIVITY_NAME
        }
    }
}
 
Example 11
Source File: ModLedControl.java    From GravityBox with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private static String getTopLevelPackageName(Context context) {
    try {
        final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName cn = taskInfo.get(0).topActivity;
        return cn.getPackageName();
    } catch (Throwable t) {
        log("Error getting top level package: " + t.getMessage());
        return null;
    }
}
 
Example 12
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 13
Source File: ContextUtils.java    From Neptune with Apache License 2.0 5 votes vote down vote up
private static String getTopActivity(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
    if (runningTaskInfos != null)
        return runningTaskInfos.get(0).topActivity.getClassName();
    else
        return null;
}
 
Example 14
Source File: Tools.java    From AndroidDemo with MIT License 5 votes vote down vote up
public static void printActivityStack(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.RunningTaskInfo runningTaskInfo = am.getRunningTasks(1).get(0);
    String packageName = runningTaskInfo.topActivity.getPackageName();
    String activityName = runningTaskInfo.topActivity.getClassName();
    StringBuilder sb = new StringBuilder();
    sb.append("package name: ").append(packageName)
            .append(" ,activity number: ").append(runningTaskInfo.numActivities)
            .append(" ,top activity: ").append(activityName);
    Log.i("zhou", sb.toString());
}
 
Example 15
Source File: AndroidUtil.java    From ThinkMap with Apache License 2.0 5 votes vote down vote up
/**
 * 检测当前App是否在前台运行
 *
 * @param context
 * @return true 前台运行,false 后台运行
 */
public static boolean isAppForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

    // 正在运行的应用
    ActivityManager.RunningTaskInfo foregroundTask = runningTasks.get(0);
    String packageName = foregroundTask.topActivity.getPackageName();
    String myPackageName = context.getPackageName();

    // 比较包名
    return packageName.equals(myPackageName);
}
 
Example 16
Source File: SchemeActivity.java    From YCVideoPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * 判断app是否正在运行
 * @param context                           上下文
 * @param packageName                       应用的包名
 * @return true 表示正在运行,false 表示没有运行
 */
public boolean isAppRunning(Context context, String packageName) {
    ActivityManager am = (ActivityManager) context.getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> 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 17
Source File: CommonUtils.java    From BigApp_WordPress_Android with Apache License 2.0 5 votes vote down vote up
public static String getTopActivity(Context context) {
    ActivityManager manager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

    if (runningTaskInfos != null)
        return runningTaskInfos.get(0).topActivity.getClassName();
    else
        return "";
}
 
Example 18
Source File: TaskUtils.java    From HelloActivityAndFragment with Apache License 2.0 5 votes vote down vote up
public static String getCurrentTopActivityName(Context context) {

        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

        //this method requires android.permission.GET_TASKS
        List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(2);
        //this method was deprecated in API level 21
        ComponentName topActivity = runningTasks.get(0).topActivity;
        Log.i(LOG_TAG, "top Activity: " + topActivity.getShortClassName());
        return topActivity.getShortClassName();

    }
 
Example 19
Source File: AutoInputAction.java    From XposedSmsCode with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private List<ActivityManager.RunningTaskInfo> getRunningTasks(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    return am == null ? null : am.getRunningTasks(10);
}
 
Example 20
Source File: VirtualApkCheckUtil.java    From EasyProtector with Apache License 2.0 2 votes vote down vote up
/**
 * TopActivity的检查顶层task的思路
 * https://github.com/109021017/android-TopActivity
 * TopActivity作为另一个进程(观察者的角度)
 * <p>
 * 能够正确识别多开软件的正确包名,类名
 * 这也是为什么能知道使用多开分身app多开后的应用包名是随机的。
 * 这里我只是提供调用方法,随时可能删掉。
 */
public String checkByTopTask(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> rtis = am.getRunningTasks(1);
    return rtis.get(0).topActivity.getPackageName();
}