ITeye博客

android由应用包名得到该应用的默认启动类

flycatdeng · 2015-09-08 21:06 · android · 移动开发 · 应用 · 默认 · Markdown

    /**
     * get the launcher activity class full name of an application by the package name
     * 
     * @param context
     *            the context of current application
     * @param packageName
     *            the package name of the application (it can be any application)
     * @return
     */
    public static String getLauncherActivityNameByPackageName(Context context, String packageName) {
        String className = null;
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);//android.intent.action.MAIN
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);//android.intent.category.LAUNCHER
        resolveIntent.setPackage(packageName);
        List<ResolveInfo> resolveinfoList = context.getPackageManager().queryIntentActivities(resolveIntent, 0);
        ResolveInfo resolveinfo = resolveinfoList.iterator().next();
        if (resolveinfo != null) {
            className = resolveinfo.activityInfo.name;
        }
        return className;
    }

 该方法可以通过某个应用的包名找到该应用中的默认启动类

        <activity
            android:name="com.fly.aty.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

 如上,将AndroidManifest.xml中的包名传入第二个参数则可以得到com.fly.aty.MainActivity

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。