Android創建和刪除桌面快捷方式

1 、創建

/**   * 爲程序創建桌面快捷方式   */   private void addShortcut(){       Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");                  //快捷方式的名稱       shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));       shortcut.putExtra("duplicate"false); //不允許重複創建                  //指定當前的Activity爲快捷方式啓動的對象: 如 com.everest.video.VideoPlayer       //注意: ComponentName的第二個參數必須加上點號(.),否則快捷方式無法啓動相應程序       ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());       shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));       //快捷方式的圖標       ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);       shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);                  sendBroadcast(shortcut);   }  

 

2、刪除

/**   * 刪除程序的快捷方式   */   private void delShortcut(){       Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");                  //快捷方式的名稱       shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));                  //指定當前的Activity爲快捷方式啓動的對象: 如 com.everest.video.VideoPlayer       //注意: ComponentName的第二個參數必須是完整的類名(包名+類名),否則無法刪除快捷方式       String appClass = this.getPackageName() + "." +this.getLocalClassName();       ComponentName comp = new ComponentName(this.getPackageName(), appClass);       shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));                  sendBroadcast(shortcut);              }  

 

3、聲明權限

在AndroidManifest.xml 文件中聲明 創建和刪除快捷方式時聲明權限

 

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

 

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