哪个发射器正在运行?

时间:2011-07-26 15:13:29

标签: android launcher

通常Android设备上有一个启动器。 但有时用户会安装一些,而只有其中一个是活动的。

如何在Android设备上查看哪个启动器当前处于活动状态?

感谢。

4 个答案:

答案 0 :(得分:24)

主屏幕以Intent ACTION_MAINCATEGORY_HOME类别(来自Intent的javadoc)启动。使用ResolveInfo来了解此意图以了解将启动哪个应用程序。

这将为您提供默认的Home应用程序:

final Intent intent = new Intent(Intent.ACTION_MAIN); 
intent.addCategory(Intent.CATEGORY_HOME); 
final ResolveInfo res = getPackageManager().resolveActivity(intent, 0); 
if (res.activityInfo == null) {
    // should not happen. A home is always installed, isn't it?
} else if ("android".equals(res.activityInfo.packageName)) {
    // No default selected     
} else {
     // res.activityInfo.packageName and res.activityInfo.name gives you the default app
} 

现在,如果您想知道哪个正在运行,则需要更多时间,因为ActivityManager很慢:

// instead of the best, query all activities that match:
final List<ResolveInfo> list = ((PackageManager)getPackageManager()).queryIntentActivities(intent, 0);
// TODO from there, use ActivityManager to know which one is running and is in the list

答案 1 :(得分:3)

private String findLauncherPackageName(){       
    final Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_HOME); 
    final ResolveInfo res = getPackageManager().resolveActivity(intent, 0); 
    Log.e("ANDRO_ASYNC", String.format("findLauncherPackageName()%s",res.activityInfo.packageName));
    return res.activityInfo.packageName;
}

答案 2 :(得分:0)

只有当标记为默认时,才可以检查活动启动器的标题。另外,你会得到像

这样的结果
com.android.internal.app.ResolverActivity

答案 3 :(得分:0)

要改进以前的答案,可以使用以下自包含的帮助程序类:

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

public class LauncherFinder {

    /**
     * Checks to see if the App currently set as the system launcher
     * matches the given package name.
     *
     * @param context
     * @param packageName
     * @return
     */
    public static boolean isPackageSetAsLauncher(Context context, String packageName) {
        String launcherPackageName = getLauncherPackageName(context);
        return packageName.equals(launcherPackageName);
    }

    /**
     * Get the package name of the app currently set as the system launcher
     * @param context
     * @return
     */
    public static String getLauncherPackageName(Context context) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);

        final PackageManager packageManager = context.getPackageManager();

        ResolveInfo result = packageManager.resolveActivity(intent, 0);

        if (result != null && result.activityInfo != null) {
            return result.activityInfo.packageName;
        }

        return null;
    }
}

,然后像这样使用它:

boolean isLauncher = LauncherFinder.isPackageSetAsLauncher(getApplicationContext(), "com.package.name");

如果当前已将 com.package.name 设置为默认启动器,则会返回 true