【问题标题】:create shortcut for thrid-party app, is that possible?为第三方应用程序创建快捷方式,这可能吗?
【发布时间】:2013-06-27 09:16:03
【问题描述】:

我使用下面的代码为我自己的应用程序创建快捷方式,我想知道我是否可以为第三方应用程序创建快捷方式?如果可以的话,我在哪里可以得到图标(Parcelable)?

    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcut.putExtra("duplicate", false);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setComponent(new ComponentName(className, activity)));
    context.sendBroadcast(shortcut);

【问题讨论】:

    标签: android shortcut


    【解决方案1】:
    public static void createShortcutForPackage(Context context, String packageName, String className) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName, className));
    
        PackageManager pm = context.getPackageManager();
        ResolveInfo ri = pm.resolveActivity(intent, 0);
    
        String shortcutName = ri.loadLabel(pm).toString();
        String activityName = ri.activityInfo.name;
        int iconId = ri.activityInfo.applicationInfo.icon;
    
        Context pkgContext = PackageUtil.createPackageContext(context, packageName);
        if (pkgContext != null) {
            ShortcutIconResource sir = Intent.ShortcutIconResource.fromContext(pkgContext, iconId);
            installShortcut(pkgContext, packageName, activityName, shortcutName, sir);
        }
    }
    
    public static void installShortcut(Context context, String packageName, String componentName, String shortcutName, Parcelable icon) {
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        ComponentName cn = new ComponentName(packageName, componentName);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(cn));
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        shortcut.putExtra("duplicate", false);
        context.sendBroadcast(shortcut);
    }
    
    public static Context createPackageContext(Context context, String pkgName) {
        Context result = null;
        try {
            result = context.createPackageContext(pkgName, Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
        } catch (NameNotFoundException e) {
            Log.d(TAG, "createPackageContext(): " + e.getStackTrace());
        }
        return result;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    相关资源
    最近更新 更多