2017-12-12 27 views
0

바로 가기가있는 메뉴를 만드는 방법을 찾으려고합니다. 내가 시도하는 경우에, 그래서하나의 활동 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

"바로 가기가 이미 만들어져 있습니다" –