2016-10-28 2 views
3

ホーム画面でユーザが押したショートカットに基づいて異なるコンテンツを表示するアクティビティがあります。ショートカットを作成するためのインテントによる1つのアクティビティのマルチショートカットの作成

のAndroidManifest.xml

<activity 
     android:name=".activities.ShortcutActivity_" 
     android:parentActivityName=".activities.MainActivity_" 
     android:screenOrientation="portrait"> 
     <intent-filter> 
      <action android:name="android.intent.action.CREATE_SHORTCUT" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

方法:

マイコードはこのようになります

private void createShortcut(String label){ 
    Intent shortcut = new Intent(getApplicationContext(), ShortcutActivity_.class); 
    shortcut.setAction(Intent.ACTION_MAIN); 

    Intent intent = new Intent(); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
      Intent.ShortcutIconResource.fromContext(getApplicationContext(), 
        R.mipmap.ic_launcher)); 
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    intent.putExtra("duplicate", false); 
    getApplicationContext().sendBroadcast(intent); 
} 

私はこのようなメソッドを呼び出しています:

createShortcut(ShortcutActivity_.class, "Shortcut 1"); 
createShortcut(ShortcutActivity_.class, "Shortcut 2"); 

私の活動では、ラベルをチェックし、ショートカットごとに異なる内容を表示したいが、機能しません。ショートカットは1つだけ作成されます。

押されたショートカットに基づいて異なるコンテンツを表示できる動的なアクティビティを作成するにはどうすればよいですか?

ご協力いただきありがとうございます。

答えて

2

私の場合、次のコードが機能しました。

private void createShortcut(Class c, String label, int id) { 
    Intent shortcut = new Intent(getApplicationContext(), c); 
    shortcut.putExtra(SHORTCUT_ID, id); 
    shortcut.putExtra("duplicate", false); 
    shortcut.setAction(Intent.ACTION_DEFAULT); 

    Intent intent = new Intent(); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
      Intent.ShortcutIconResource.fromContext(getApplicationContext(), 
        R.mipmap.ic_launcher)); 
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    intent.putExtra("duplicate", false); 
    getApplicationContext().sendBroadcast(intent); 
} 

あなたの活動では、このような方法で起動します

createShortcut(ShortcutActivity.class, "Shortcut 2", 2); 

そして、あなたのShortcutActivityに、このような意図を取得し、それに応えています。

Intent intent = getIntent(); 
    if (intent == null) 
     return; 

    if (intent.getIntExtra(MainActivity.SHORTCUT_ID, 0) == 1){ 
     textView2.setText("Shortcut 1"); 
    } 
    if (intent.getIntExtra(MainActivity.SHORTCUT_ID, 0) == 2){ 
     textView2.setText("Shortcut 2"); 
    } 
関連する問題