Android检查仅桌面正在运行

时间:2014-12-09 07:45:08

标签: android android-activity

我正在编写简单的SMS管理器,我的应用程序可以查看用于接收新的短信的对话框,就像使用GoSMS一样,现在我想在其他应用程序运行时禁用它,我想在没有打开程序和启动器时查看此对话框活性。我可以使用下面的代码来检测它,但如果用户安装了任何启动器我怎么知道。

    ActivityManager am = (ActivityManager) G.context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    String currentRunningActivityName = taskInfo.get(0).topActivity.getClassName();
    Log.i(TAG, currentRunningActivityName);
    if (currentRunningActivityName.equals("com.android.launcher2.Launcher")) {
        ///do your task
    }

1 个答案:

答案 0 :(得分:1)

您可以使用此代码段,此方法可以找到所有启动器,如果返回true,则默认情况下桌面启动器正在运行:

public static Boolean isHomeScreen() {
    ActivityManager am = (ActivityManager) G.context.getSystemService( Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    String currentActivity = taskInfo.get(0).topActivity.getClassName();

    PackageManager pm = G.context.getPackageManager();
    Intent homeIntent = new Intent(Intent.ACTION_MAIN, null);
    homeIntent.addCategory(Intent.CATEGORY_DEFAULT);
    homeIntent.addCategory(Intent.CATEGORY_HOME);
    homeIntent.addCategory(Intent.CATEGORY_MONKEY);
    List<ResolveInfo> appList = pm.queryIntentActivities(homeIntent, 0);
    for (ResolveInfo temp: appList) {
        if (temp.activityInfo.name.equals(currentActivity))
            return true;
    }
    return false;
}
相关问题