天天看點

Android建立和删除桌面快捷方式

有同學方回報建立快捷方式後,點選快捷方式後不能啟動程式或者提示"未安裝程式",貌似是新的rom在快捷方式這塊做過修改(由于此文是11年5月所出,估計應該是2.0或2.1的rom),現已修正,HTC G11 2.3.5rom測試通過.

1,判斷是否已經建立了快捷方式(在某些機型中需要判斷)

  1. private boolean hasShortcut()  
  2. {  
  3.         boolean isInstallShortcut = false;  
  4.         final ContentResolver cr = activity.getContentResolver();  
  5.         final String AUTHORITY ="com.android.launcher.settings";  
  6.         final Uri CONTENT_URI = Uri.parse("content://" +AUTHORITY + "/favorites?notify=true");  
  7.         Cursor c = cr.query(CONTENT_URI,new String[] {"title","iconResource" },"title=?",  
  8.         new String[] {mapViewActivity.getString(R.string.app_name).trim()}, null);  
  9.         if(c!=null && c.getCount()>0){  
  10.             isInstallShortcut = true ;  
  11.         }  
  12.         return isInstallShortcut ;  
  13.     } 

2, 建立

  1. /**   
  2.      * 為程式建立桌面快捷方式   
  3.      */   
  4.     private void addShortcut(){    
  5.         Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");    
  6.         //快捷方式的名稱    
  7.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));    
  8.         shortcut.putExtra("duplicate", false); //不允許重複建立    
  9.         /****************************此方法已失效*************************/ 
  10.         //ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());    
  11.         //shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));      
  12.      /******************************end*******************************/ 
  13.      Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);  
  14.      shortcutIntent.setClassName(this, this.getClass().getName());  
  15.      shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);  
  16.         //快捷方式的圖示    
  17.         ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);    
  18.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);                   
  19.         sendBroadcast(shortcut);    
  20.     }   

3, 删除

  1. /**   
  2.    * 删除程式的快捷方式   
  3.    */   
  4.   private void delShortcut(){    
  5.       Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");    
  6.       //快捷方式的名稱    
  7.       shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));    
  8.       String appClass = this.getPackageName() + "." +this.getLocalClassName();    
  9.       ComponentName comp = new ComponentName(this.getPackageName(), appClass);    
  10.       shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));    
  11.       sendBroadcast(shortcut);             
  12.   }   
  1. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />    
  2. <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />