标签:
1 /** 2 * 3 * @param context 4 * @param tname 桌面快捷方式的名字 5 * @param icon 好友头像 6 */ 7 public static void addShortCut(Context context, String tname, int icon) { 8 // 安装的Intent 9 Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 10 11 // 快捷名称 12 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, tname); 13 // 快捷图标是允许重复 14 shortcut.putExtra("duplicate", false); 15 16 Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); 17 shortcutIntent.putExtra("tName", tname); 18 shortcutIntent.setClassName("com.example.qqshortcut", "com.example.qqshortcut.MainActivity"); 19 shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 20 shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 21 22 // 快捷图标 23 ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, icon); 24 shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); 25 26 // 发送广播 27 context.sendBroadcast(shortcut); 28 }
1 /** 2 * 判断是否已存在该快捷方式 3 * @param context 4 * @param name 快捷方式的名字 5 * @return 6 */ 7 public static boolean hasShortcut(Context context,String name) 8 { 9 String authority = getAuthorityFromPermission(context,"permission.READ_SETTINGS"); 10 boolean isInstallShortcut = false; 11 try { 12 ContentResolver cr = context.getContentResolver(); 13 String url; 14 if (authority == null) { 15 int systemversion = Integer.parseInt(android.os.Build.VERSION.SDK); 16 /*大于8的时候在com.android.launcher2.settings 里查询(未测试)*/ 17 if(systemversion < 8){ 18 url = "content://com.android.launcher.settings/favorites?notify=true"; 19 }else{ 20 url = "content://com.android.launcher2.settings/favorites?notify=true"; 21 } 22 }else{ 23 url = "content://" + authority + "/favorites?notify=true"; 24 } 25 Cursor c = cr.query(Uri.parse(url),null,"title=?", 26 new String[] {name}, null); 27 if(c!=null && c.moveToFirst()){ 28 c.close(); 29 isInstallShortcut = true ; 30 } 31 } catch (Exception e) { 32 e.printStackTrace(); 33 } 34 return isInstallShortcut ; 35 }
1 /** 2 * 动态获取查询的provider(provider跟SDK版本和厂商有关系,会有所不同),获取provider后,记得在androidManifeset.xml加上对应的权限 3 * 不同厂商com.android.launcher.permission.READ_SETTINGS权限不同,目前做兼容只能把目前知道的权限都加到配置文件里 4 * @param context 5 * @param permission 6 * @return 7 */ 8 public static String getAuthorityFromPermission(Context context, String permission){ 9 try { 10 if (permission == null) return null; 11 List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS); 12 if (packs != null) { 13 for (PackageInfo pack : packs) { 14 ProviderInfo[] providers = pack.providers; 15 if (providers != null) { 16 for (ProviderInfo provider : providers) { 17 Log.e("------", "\r\n【" + provider.readPermission + "】\r\n"); 18 if (provider.readPermission!=null && provider.readPermission.endsWith(permission)) { 19 return provider.authority; 20 } 21 } 22 } 23 } 24 } 25 } catch (Exception e) { 26 e.printStackTrace(); 27 } 28 return null; 29 }
标签:
原文地址:http://www.cnblogs.com/kelina2mark/p/4522934.html