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

有同窗方反馈建立快捷方式后,点击快捷方式后不能启动程序或者提示"未安装程序",貌似是新的rom在快捷方式这块作过修改(因为此文是11年5月所出,估计应该是2.0或2.1的rom),现已修正,HTC G11 2.3.5rom测试经过.android

1,判断是否已经建立了快捷方式(在某些机型中须要判断)app

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

2, 建立ide

  
  
  
  
  1.  
  2. /**   
  3.  
  4.      * 为程序建立桌面快捷方式   
  5.  
  6.      */   
  7.  
  8.     private void addShortcut(){    
  9.  
  10.         Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");    
  11.  
  12.                  
  13.  
  14.         //快捷方式的名称    
  15.  
  16.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));    
  17.  
  18.         shortcut.putExtra("duplicate"false); //不容许重复建立    
  19.  
  20.     
  21.  
  22.         /****************************此方法已失效*************************/ 
  23.  
  24.         //ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());    
  25.  
  26.         //shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));      
  27.  
  28.      /******************************end*******************************/ 
  29.  
  30.      Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);  
  31.  
  32.      shortcutIntent.setClassName(thisthis.getClass().getName());  
  33.  
  34.      shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);  
  35.  
  36.    
  37.  
  38.         //快捷方式的图标    
  39.  
  40.         ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);    
  41.  
  42.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);                   
  43.  
  44.         sendBroadcast(shortcut);    
  45.  
  46.     }   

3, 删除测试

  
  
  
  
  1. /**   
  2.  
  3.    * 删除程序的快捷方式   
  4.  
  5.    */   
  6.  
  7.   private void delShortcut(){    
  8.  
  9.       Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");    
  10.  
  11.                
  12.  
  13.       //快捷方式的名称    
  14.  
  15.       shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));    
  16.  
  17.       String appClass = this.getPackageName() + "." +this.getLocalClassName();    
  18.  
  19.       ComponentName comp = new ComponentName(this.getPackageName(), appClass);    
  20.  
  21.       shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));    
  22.  
  23.       sendBroadcast(shortcut);             
  24.  
  25.   }   

3, 声明权限this

在AndroidManifest.xml 文件中声明 建立和删除快捷方式时声明权限spa

  
  
  
  
  1.  
  2. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />    
  3.  
  4. <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
相关文章
相关标签/搜索