2016-08-20 76 views
-1

的名字,我想創建一個應用程序,它會創建一個用戶輸入快捷方式名稱相同的應用程序的多個快捷方式。但每個快捷方式都會執行不同的指定操作。爲此,我需要知道快捷方式的名稱。越來越安卓快捷

+0

的可能重複的[添加快捷方式Android應用程序要在主屏幕上按一下按鈕(http://stackoverflow.com/questions/10343414/add-shortcut-for-android-application-to-home-screen-on-按鈕單擊) –

+0

Hi @Ratul!你有沒有找到一種方法來做到這一點? –

回答

0

首先,你需要的INSTALL_SHORTCUT許可

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

您可以使用下面的代碼,並進行更改

EditText UserInput = (EditText)findViewById(R.id.userinput_text); 
final String UserString = UserInput.getText().toString(); 
//Gets user input^ 

public void createIcon(){ 
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 

    //So the shortcut doesn't get created multiple times 
    shortcutintent.putExtra("duplicate", false); 

    //UserString is the text from the user input to set the shortcut name 
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(UserString)); 

    //set the icon picture 
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon); 

    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new 

    //Set the Activity that will be opened 
    Intent(getApplicationContext(), EnterActivity.class)); 

    sendBroadcast(shortcutintent); 
} 

這將快捷方式添加到啓動程序,並從你的應用程序打開一個自定義的活動。我不確定你真的想做什麼,因爲它不清楚你要求什麼。