首先要宣告
1.AndroidManifest.xml
2.MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addShortcut();
}
/* 建立桌面捷徑 */
private void addShortcut() {
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class); // 啟動捷徑入口,一般用MainActivity,有使用其他入口則填入相對名稱,ex:有使用SplashScreen
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); // shortcutIntent送入
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name)); // 捷徑app名稱
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(
getApplicationContext(),// 捷徑app圖
R.drawable.ic_launcher));
addIntent.putExtra("duplicate", false); // 只創建一次
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); // 安裝
getApplicationContext().sendBroadcast(addIntent); // 送出廣播
}
參考來源:http://stackoverflow.com/questions/22829647/programmatically-add-the-widget-to-home-screen-in-android
http://stackoverflow.com/questions/21542409/home-screen-app-shortcut-is-not-working-android-app-isnt-installed
感謝你的分享
回覆刪除請問能否再安裝完成以後自動安裝,目前只有安裝完成,點開APP的瞬間會安裝捷徑
應該不行喔,因為程式碼是在啟動App時才會執行。
刪除