Android添加快捷方式(Short)到手机桌面

这篇博客介绍了如何在Android应用中添加、移除和查询手机桌面快捷方式。添加快捷方式需要在manifest中声明权限,并通过发送ACTION_ADD_SHORTCUT广播实现;移除快捷方式则使用ACTION_REMOVE_SHORTCUT;查询快捷方式的方法在部分设备上可能无法工作。
摘要由CSDN通过智能技术生成

权限

  要在手机桌面上添加快捷方式,首先需要在manifest中添加权限。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- 添加快捷方式 -->  
  2. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />  
  3. <!-- 移除快捷方式 -->  
  4. <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />  
  5. <!-- 查询快捷方式 -->  
  6. <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />  

添加快捷方式

  添加快捷方式,是向桌面应用(launcher)发送相关action的广播,相关的action如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";  

       添加快捷方式:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.  private void addShortcut(String name) {  
  2.     Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT);  
  3.   
  4.     // 不允许重复创建  
  5.     addShortcutIntent.putExtra("duplicate"false);// 经测试不是根据快捷方式的名字判断重复的  
  6.     // 应该是根据快链的Intent来判断是否重复的,即Intent.EXTRA_SHORTCUT_INTENT字段的value  
  7.     // 但是名称不同时,虽然有的手机系统会显示Toast提示重复,仍然会建立快链  
  8.     // 屏幕上没有空间时会提示  
  9.     // 注意:重复创建的行为MIUI和三星手机上不太一样,小米上似乎不能重复创建快捷方式  
  10.   
  11.     // 名字  
  12.     addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);  
  13.   
  14.     // 图标  
  15.     addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,  
  16.             Intent.ShortcutIconResource.fromContext(MainActivity.this,  
  17.                     R.drawable.ic_launcher));  
  18.   
  19.     // 设置关联程序  
  20.     Intent launcherIntent = new Intent(Intent.ACTION_MAIN);  
  21.     launcherIntent.setClass(MainActivity.this, MainActivity.class);  
  22.     launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
  23.   
  24.     addShortcutIntent  
  25.             .putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);  
  26.   
  27.     // 发送广播  
  28.     sendBroadcast(addShortcutIntent);  
  29. }  

移除快捷方式

  移除快捷方式的action:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static final String ACTION_REMOVE_SHORTCUT = "com.android.launcher.action.UNINSTALL_SHORTCUT";  
        移除快捷方式的方法:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. private void removeShortcut(String name) {  
  2.     // remove shortcut的方法在小米系统上不管用,在三星上可以移除  
  3.     Intent intent = new Intent(ACTION_REMOVE_SHORTCUT);  
  4.   
  5.     // 名字  
  6.     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);  
  7.   
  8.     // 设置关联程序  
  9.     Intent launcherIntent = new Intent(MainActivity.this,  
  10.             MainActivity.class).setAction(Intent.ACTION_MAIN);  
  11.   
  12.     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);  
  13.   
  14.     // 发送广播  
  15.     sendBroadcast(intent);  
  16. }  
       在两个手机上测试,发现小米手机上添加了快捷方式后不能移除,三星手机可以。

查询快捷方式

  查询快捷方式是否存在的方法是从网上其他资料那里查来的,但是测试查询的时候失败了,两个手机(小米、三星)都查不到。

  先留着代码以后看看是什么原因吧:


      
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. private boolean hasInstallShortcut(String name) {  
  2.   
  3.     boolean hasInstall = false;  
  4.   
  5.     final String AUTHORITY = "com.android.launcher2.settings";  
  6.     Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY  
  7.             + "/favorites?notify=true");  
  8.   
  9.     // 这里总是failed to find provider info  
  10.     // com.android.launcher2.settings和com.android.launcher.settings都不行  
  11.     Cursor cursor = this.getContentResolver().query(CONTENT_URI,  
  12.             new String[] { "title""iconResource" }, "title=?",  
  13.             new String[] { name }, null);  
  14.   
  15.     if (cursor != null && cursor.getCount() > 0) {  
  16.         hasInstall = true;  
  17.     }  
  18.   
  19.     return hasInstall;  
  20.   
  21. }  


      转自:http://www.cnblogs.com/mengdd/p/3837592.html
0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值