顯示具有 捷徑 標籤的文章。 顯示所有文章
顯示具有 捷徑 標籤的文章。 顯示所有文章

2014年6月16日 星期一

Android shortcut 桌面 捷徑

自動建立桌面捷徑的方法,使用shortcut
首先要宣告
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