Android 創建,驗證和刪除桌面快捷方式 (刪除快捷方式測試可用)


測試環境為Adnroid 2.1以上。

第一步:AndroidManifest.xml 權限配置:

添加快捷方式權限:

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

驗證快捷方式是否存在權限:
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

刪除快捷方式權限:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

代碼:

 1 public class ShortCutSample {
 2     /**
 3     * 添加快捷方式
 4     * */
 5     public void creatShortCut(Activity activity,String shortcutName,int resourceId)
 6     {
 7         Intent intent = new Intent(); 
 8         intent.setClass(activity, activity.getClass());  
 9         /*以下兩句是為了在卸載應用的時候同時刪除桌面快捷方式*/
10         intent.setAction("android.intent.action.MAIN");  
11         intent.addCategory("android.intent.category.LAUNCHER");  
12         
13         Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
14          
15          //不允許重復創建
16          shortcutintent.putExtra("duplicate", false);
17          //需要現實的名稱
18          shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
19          //快捷圖片
20          Parcelable icon = Intent.ShortcutIconResource.fromContext(activity.getApplicationContext(), resourceId);
21          shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
22          //點擊快捷圖片,運行的程序主入口
23          shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
24          //發送廣播。OK
25          activity.sendBroadcast(shortcutintent);
26     }
27     /**
28     * 刪除快捷方式
29     * */
30     public void deleteShortCut(Activity activity,String shortcutName)
31     {
32         Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");  
33         //快捷方式的名稱  
34         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,shortcutName);  
35         //在網上看到到的基本都是一下幾句,測試的時候發現並不能刪除快捷方式。
36         //String appClass = activity.getPackageName()+"."+ activity.getLocalClassName();  
37         //ComponentName comp = new ComponentName( activity.getPackageName(), appClass);  
38         //shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));   
39         /**改成以下方式能夠成功刪除,估計是刪除和創建需要對應才能找到快捷方式並成功刪除**/
40         Intent intent = new Intent(); 
41         intent.setClass(activity, activity.getClass());  
42         intent.setAction("android.intent.action.MAIN");  
43         intent.addCategory("android.intent.category.LAUNCHER");  
44         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);  
45         activity.sendBroadcast(shortcut);          
46     }
47     /**
48     * 判斷是否存在快捷方式
49     * */
50     public boolean hasShortcut(Activity activity,String shortcutName)
51     {
52         String url = ""; 
53         int systemversion = Integer.parseInt(android.os.Build.VERSION.SDK);
54         /*大於8的時候在com.android.launcher2.settings 里查詢(未測試)*/
55         if(systemversion < 8){ 
56               url = "content://com.android.launcher.settings/favorites?notify=true"; 
57         }else{ 
58             url = "content://com.android.launcher2.settings/favorites?notify=true"; 
59         } 
60         ContentResolver resolver = activity.getContentResolver(); 
61         Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",new String[] {shortcutName}, null); 
62         if (cursor != null && cursor.moveToFirst()) { 
63                 cursor.close(); 
64                 return true; 
65         } 
66         return false; 
67     }
68 }

調用測試代碼:

   public class mainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ShortCutSample sample =new ShortCutSample();
        String shortcutName=getString(R.string.app_name);
        
        if(sample.hasShortcut(this, shortcutName))
        	sample.deleteShortCut(this,shortcutName);
        else
        	sample.creatShortCut(this,shortcutName,R.drawable.icon);
        
    }
}

在網上找了很久都是一樣的代碼,刪除那塊搞了一個下午才弄好,其實很簡單的東東。

第一次發文章,Adnroid新人。多多交流和指導呀。呵呵。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM