6

以下は、選択したアプリケーションへのショートカットを作成するための私のコードです。私は本当に問題はなく、アプリケーションは非常にうまく機能します。

問題は、アプリケーションからリソースを使用してショートカットを作成できることです。

    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));

しかし、私は本当にカスタムドローアブルが欲しいです。(ドローアブル myDrawable=.....)

どのようにできるのか?

   ResolveInfo launchable=adapter.getItem(position);
   final Intent shortcutIntent = new Intent();
    ActivityInfo activity=launchable.activityInfo;
    ComponentName name=new ComponentName(activity.applicationInfo.packageName,activity.name);       
    shortcutIntent.setComponent(name);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // Sets the custom shortcut's title
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, launchable.loadLabel(pm));
    // Set the custom shortcut icon
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));

    // add the shortcut
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(intent);
    finish();

手がかりをどうもありがとう

4

2 に答える 2

28

最後に解決策を見つけました。Intent.EXTRA_SHORTCUT_ICON_RESOURCE を使用するのは愚かでした:

正しいコードは次のとおりです。

Drawable iconDrawable = (....); 
BitmapDrawable bd = (BitmapDrawable) iconDrawable;
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bd.getBitmap());
于 2010-07-25T22:02:14.173 に答える
3

評判が23しかないのでコメントできませんが、あなたのコードを使用しましたが、役に立ちました。しかし、私の画像はショートカットで正しく拡大縮小されなかったので、解決策を完成させることができる 2 つの興味深いトピックを見つけました。

ショートカット アイコンの正しいサイズを取得するには: https://stackoverflow.com/a/19003905/3741926

ドローアブルをスケーリングするには (前の関数で計算したサイズを使用) https://stackoverflow.com/a/10703256/3741926

正しくスケーリングされたアイコンを持つショートカットを取得したすべてで、それが役立つことを願っています

于 2014-11-25T22:49:24.393 に答える