7

重複の可能性:
Android はホーム画面にショートカットを作成します

私はそれを持っていTextViewButton、私のアクティビティ(HomeActivity.class)にいます。

目標: をクリックButtonすると、デフォルトの Android イメージをアイコンとして、テキストを に入力したショートカットが作成されますTextView

これまでのところ、次を使用してショートカットを作成できることがわかりましたACTION_CREATE_SHORTCUT

これは私がこれまでに持っているコードです:

Intent result = new Intent(android.content.Intent.ACTION_CREATE_SHORTCUT);
result.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
                new Intent(getApplicationContext(), Editor.class));
result.putExtra(TodoDbAdapter.KEY_ROWID,rowid);
result.putExtra(Intent.EXTRA_SHORTCUT_NAME,textView.getText());
result.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(HomeActivity.this,
                                                        R.drawable.icon));
setResult(RESULT_OK, result);

私のマニフェストファイル:

<activity android:name=".HomeActivity" android:label="@string/app_name">

        <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

ただし、このコードはショートカットを作成しません。

ショートカットを作成するにはどうすればよいですか?

4

1 に答える 1

18

これを試して:

Intent shortcutIntent;
shortcutIntent = new Intent();
shortcutIntent.setComponent(new ComponentName(activity.getPackageName(), ".classname"));

shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

final Intent putShortCutIntent = new Intent();
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

// Sets the custom shortcut's title
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"Title");
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(PersonProfile.this, R.drawable.icon));
putShortCutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(putShortCutIntent);

この権限をマニフェストに追加します。

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
于 2011-06-24T04:47:30.737 に答える