创建THRID第三方应用程序的快捷方式,这可能吗? [英] create shortcut for thrid-party app, is that possible?

查看:234
本文介绍了创建THRID第三方应用程序的快捷方式,这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 意图快捷=新意图(com.android.launcher.action.INSTALL_SHORTCUT);
    shortcut.putExtra(重复,FALSE);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,名);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,图标);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,新意图()setComponent(新组件名(类名,活动)));
    context.sendBroadcast(快捷方式);


解决方案

 公共静态无效createShortcutForPackage(上下文的背景下,字符串的packageName,弦乐的className){
    意向意图=新的Intent();
    intent.setComponent(新组件名(的packageName,类名));    软件包管理系统下午= context.getPackageManager();
    ResolveInfoり= pm.resolveActivity(意向,0);    字符串shortcutName = ri.loadLabel(PM)的ToString();
    字符串activityName = ri.activityInfo.name;
    INT iconId = ri.activityInfo.applicationInfo.icon;    上下文pkgContext = PackageUtil.createPackageContext(背景下,的packageName);
    如果(pkgContext!= NULL){
        ShortcutIconResource先生= Intent.ShortcutIconResource.fromContext(pkgContext,iconId);
        installShortcut(pkgContext,的packageName,activityName,shortcutName,先生);
    }
}公共静态无效installShortcut(上下文的背景下,字符串的packageName,串ComponentName,字符串shortcutName,Parcelable图标){
    意图快捷=新意图(com.android.launcher.action.INSTALL_SHORTCUT);
    组件名CN =新的组件名(的packageName,组件名称);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,新的意图(Intent.ACTION_MAIN).setComponent(CN));
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,shortcutName);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,图标);
    shortcut.putExtra(重复,FALSE);
    context.sendBroadcast(快捷方式);
}公共静态上下文createPackageContext(上下文的背景下,字符串PKGNAME){
    上下文结果= NULL;
    尝试{
        结果= context.createPackageContext(PKGNAME,Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_ code);
    }赶上(ē的NameNotFoundException){
        Log.d(TAGcreatePackageContext():+ e.getStackTrace());
    }
    返回结果;
}

I use code below to create shortcut for my own app, and I wonder if I can create a shortcut for third-party app? If that's possible, Where could I get the icon(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);

解决方案

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;
}

这篇关于创建THRID第三方应用程序的快捷方式,这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆