0

ホーム画面に配置できるウィジェットを作成できることはわかっていますが、ユーザーがアプリをインストールすると、標準のランチャー アイコンだけが特定のアクティビティを開始する可能性があります。しかし、ユーザーがそのように選択すると (たとえば、アプリのボタンをクリックして)、デバイスのホーム画面に別のアイコンが作成され、別のアクティビティに直接リンクされますか? ホーム画面でそのアイコンをクリックすると、パッケージ内の別のアクティビティが開きますか?

可能であれば、誰かがスニペットを持っていますか?

ありがとう!

4

1 に答える 1

2

このブログのおかげで: http://viralpatel.net/blogs/android-install-uninstall-shortcut-example/

マニフェストで、必要なアクセス許可を追加します。

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

ショートカットが参照しているマニフェストのアクティビティに追加します。

   android:exported="true"

次に、次の方法を使用してショートカットをインストール/アンインストールします。

 private void addShortcut() {
        //Adding shortcut for MainActivity 
        //on Home screen
        Intent shortcutIntent = new Intent(getApplicationContext(),
                MainActivity.class);

        shortcutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent
                .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                        R.drawable.ic_launcher));

        addIntent
                .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }


private void removeShortcut() {

        //Deleting shortcut for MainActivity 
        //on Home screen
        Intent shortcutIntent = new Intent(getApplicationContext(),
                MainActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent
                .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");

        addIntent
                .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }

ショートカット メニューにアクティビティを追加するには、このインテント フィルターをマニフェストのアクティビティに追加するだけです。

<intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
于 2013-02-02T17:51:15.290 に答える