Android 创建,验证和删除桌面快捷方式 (删除快捷方式测试可用)

原文地址:http://www.cnblogs.com/yeqw1985/archive/2013/02/06/2907704.html

 

 

测试环境为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新人。多多交流和指导呀。呵呵。

你可能感兴趣的:(android)