创建和删除快捷方式以及判断是否有快捷方式

来源:互联网 发布:优化网站的文件和资源 编辑:程序博客网 时间:2024/04/20 02:13

Launcher为了应用程序能够定制自己的快捷图标,就注册了一个BroadcastReceiver专门接收其他应用程序发来的快捷图标定制信息。所以只需要根据该BroadcastReceiver构造出相对应的Intent并装入我们的定制信息,最后调用sendBroadcast方法就可以创建一个快捷图标了。既然可以创建快捷方式也就可以删除快捷方式,以及判断快捷方式是否存在。

如何向这个 BroadcastReceiver 发送广播,设置如下:

  1. 创建快捷方式必须要有权限;
  2. 创建快捷方式的广播的Intent的action设置com.android.launcher.action.INSTALL_SHORTCUT
  3. 删除快捷方式的广播的Intent的action设置com.android.launcher.action.UNINSTALL_SHORTCUT
  4. 设置快捷方式的图片和名称等信息放在Intent中;

快捷图标的信息则是以附加信息的形式存储在广播出去的Intent对象中的,包括有图标、显示的名称以及用来启动目标组件的Intent这三种信息。我们可以通过putExtra的重载方法,通过指定相应的键值,将这些信息放到附加信息的Bundle对象中。

下面是快捷方式的工具类,可以创建快捷方式,删除快捷方式等方法。

[java] view plaincopy
  1. /** 
  2.  * @author wangli 快捷方式工具类 
  3.  */  
  4. public class ShortCutUtils {  
  5.     /** 
  6.      * 添加当前应用的桌面快捷方式 
  7.      *  
  8.      * @param cx 
  9.      */  
  10.     public static void addShortcut(Context cx) {  
  11.         Intent shortcut = new Intent(  
  12.                 "com.android.launcher.action.INSTALL_SHORTCUT");  
  13.   
  14.         Intent shortcutIntent = cx.getPackageManager()  
  15.                 .getLaunchIntentForPackage(cx.getPackageName());  
  16.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);  
  17.         // 获取当前应用名称  
  18.         String title = null;  
  19.         try {  
  20.             final PackageManager pm = cx.getPackageManager();  
  21.             title = pm.getApplicationLabel(  
  22.                     pm.getApplicationInfo(cx.getPackageName(),  
  23.                             PackageManager.GET_META_DATA)).toString();  
  24.         } catch (Exception e) {  
  25.         }  
  26.         // 快捷方式名称  
  27.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);  
  28.         // 不允许重复创建(不一定有效)  
  29.         shortcut.putExtra("duplicate"false);  
  30.         // 快捷方式的图标  
  31.         Parcelable iconResource = Intent.ShortcutIconResource.fromContext(cx,  
  32.                 R.drawable.ic_launcher);  
  33.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);  
  34.   
  35.         cx.sendBroadcast(shortcut);  
  36.     }  
  37.   
  38.     /** 
  39.      * 删除当前应用的桌面快捷方式 
  40.      *  
  41.      * @param cx 
  42.      */  
  43.     public static void delShortcut(Context cx) {  
  44.         Intent shortcut = new Intent(  
  45.                 "com.android.launcher.action.UNINSTALL_SHORTCUT");  
  46.   
  47.         // 获取当前应用名称  
  48.         String title = null;  
  49.         try {  
  50.             final PackageManager pm = cx.getPackageManager();  
  51.             title = pm.getApplicationLabel(  
  52.                     pm.getApplicationInfo(cx.getPackageName(),  
  53.                             PackageManager.GET_META_DATA)).toString();  
  54.         } catch (Exception e) {  
  55.         }  
  56.         // 快捷方式名称  
  57.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);  
  58.         Intent shortcutIntent = cx.getPackageManager()  
  59.                 .getLaunchIntentForPackage(cx.getPackageName());  
  60.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);  
  61.         cx.sendBroadcast(shortcut);  
  62.     }  
  63.   
  64.     /** 
  65.      * 判断当前应用在桌面是否有桌面快捷方式 
  66.      *  
  67.      * @param cx 
  68.      */  
  69.     public static boolean hasShortcut(Context cx) {  
  70.         boolean result = false;  
  71.         String title = null;  
  72.         try {  
  73.             final PackageManager pm = cx.getPackageManager();  
  74.             title = pm.getApplicationLabel(  
  75.                     pm.getApplicationInfo(cx.getPackageName(),  
  76.                             PackageManager.GET_META_DATA)).toString();  
  77.         } catch (Exception e) {  
  78.         }  
  79.   
  80.         final String uriStr;  
  81.         if (android.os.Build.VERSION.SDK_INT < 8) {  
  82.             uriStr = "content://com.android.launcher.settings/favorites?notify=true";  
  83.         } else {  
  84.             uriStr = "content://com.android.launcher2.settings/favorites?notify=true";  
  85.         }  
  86.         final Uri CONTENT_URI = Uri.parse(uriStr);  
  87.         final Cursor c = cx.getContentResolver().query(CONTENT_URI, null,  
  88.                 "title=?"new String[] { title }, null);  
  89.         if (c != null && c.getCount() > 0) {  
  90.             result = true;  
  91.         }  
  92.         return result;  
  93.     }  
  94. }  

其中涉及的3个权限

[html] view plaincopy
  1. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />  
  2. <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />  
  3. <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />  

通过这个类,你就可以轻而易举的为应用程序添加快捷图标。

0 0
原创粉丝点击