創建桌面快捷方式

//設置快捷方式要跳轉的activity
  Intent intent = new Intent();
  intent.setClass(this, SplashActivity.class);
  
  //要加上下面兩句,否則應用被卸載之後,快捷方式不會消失
//  intent.setAction("android.intent.action.MAIN");
//  intent.addCategory("android.intent.category.LAUNCHER");

  Intent addShortcut = new Intent(
    "com.android.launcher.action.INSTALL_SHORTCUT");
  Parcelable icon = Intent.ShortcutIconResource.fromContext(this,
    R.drawable.icon);//快捷方式圖標
  addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
    getString(R.string.app_name));//設置名稱
  addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);//點擊快捷方式後執行的操作
  addShortcut.putExtra("duplicate", false);//是否允許快捷方式重複
  addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
  sendBroadcast(addShortcut);

發佈了63 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章