天天看点

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" />