2017-12-12 24 views
0

ショートカットを使用してメニューを作成する方法を見つけようとしています。私がしようとした場合、だから、1つのアクティビティAppの複数のショートカット

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"); 
    addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate 
    getApplicationContext().sendBroadcast(addIntent); 
} 

:私はこのtopicからこのメソッドを持っ

enter image description here

例えば

Activity負荷fragmentIntentに来るデータに依存しますMainActivityの2つのショートカットを作成するには、「ショートカットがすでに作成されています」というメッセージが表示されます。

ありがとうございます。

+0

下記のリンクがあなたにいくつかの手がかりを与えてくれることを願っています。 [https://stackoverflow.com/questions/11240023/two-launchers-for-a-single-activity](https://stackoverflow.com/questions/11240023/two-launchers-for-a-single-activity) –

答えて

0

複数のショートカットを作成するとこれが試されます。

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" /> 

ハッピーコーディング!

+0

"ショートカットが既に作成されています" –

関連する問題