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" />
相關文章
相關標籤/搜索