ANDROID 桌面快捷方式创建,和判断 返回false问题
2014-06-06 22:24
417 查看
创建快捷方式
一般要判断下是否已经创建了
因为不同的手机厂商可能对手机系统进行了修改使用原生的
“content://com.android.launcher.settings/favorites?notify=true"
或者
"content://com.android.launcher2.settings/favorites?notify=true"
并不能准确判断 需要通过权限去获取当前手机provider.authority
权限配置,要在创建快捷方式的activity加 <action android:name="android.intent.action.CREATE_SHORTCUT"></action>
/** * 创建快捷方式 */ public static void addShortcut(Activity context,Class calss) { if(isAddShortCut(context)) return ; Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); // 设置属性 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context. getResources().getString(R.string.app_name)); ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context.getApplicationContext(), R.drawable.ic_launcher); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes); // 是否允许重复创建 shortcut.putExtra("duplicate", false); //设置桌面快捷方式的图标 Parcelable icon = Intent.ShortcutIconResource.fromContext(context,R.drawable.ic_launcher); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon); //点击快捷方式的操作 Intent intent = new Intent(Intent.ACTION_MAIN); intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setClass(context,calss); // 设置启动程序 shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); //广播通知桌面去创建 context.sendBroadcast(shortcut); }
一般要判断下是否已经创建了
public static boolean isAddShortCut(Activity context) { String url = ""; try { url = "content://" + getAuthorityFromPermission(context, "com.android.launcher.permission.READ_SETTINGS") + "/favorites?notify=true"; ContentResolver resolver = context.getContentResolver(); Cursor cursor = resolver.query(Uri.parse(url), new String[] { "title", "iconResource" }, "title=?", new String[] { context.getString(R.string.app_name).trim() }, null); if (cursor != null && cursor.moveToFirst()) { cursor.close(); return true; } } catch (Exception e) { e.printStackTrace(); } return false; }
因为不同的手机厂商可能对手机系统进行了修改使用原生的
“content://com.android.launcher.settings/favorites?notify=true"
或者
"content://com.android.launcher2.settings/favorites?notify=true"
并不能准确判断 需要通过权限去获取当前手机provider.authority
/** * 通过权限去获取当前手机provider.authority * @param context * @param permission * @return */ public static String getAuthorityFromPermission(Context context, String permission) { if (permission == null) return null; List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS); if (packs != null) { for (PackageInfo pack : packs) { ProviderInfo[] providers = pack.providers; if (providers != null) { for (ProviderInfo provider : providers) { if (permission.equals(provider.readPermission)) return provider.authority; if (permission.equals(provider.writePermission)) return provider.authority; } } } } return null; }
权限配置,要在创建快捷方式的activity加 <action android:name="android.intent.action.CREATE_SHORTCUT"></action>
<activity android:name="com.hw.weather.fuzhou.LaunchActivity" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT"></action> </intent-filter> </activity>
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <!-- 创建删除快捷方式 --> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> <!-- 创建删除快捷方式 MOTO的部分机型还要加上以下权限 --> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />
相关文章推荐
- Android添加(创建)、删除及判断是否存在桌面快捷方式的方法
- 解决三星I9000手机下Android版UCWEB老是提示创建桌面快捷方式的问题
- [转] Android应用添加(创建)和删除及判断是否存在桌面快捷方式
- Android应用添加(创建)和删除及判断是否存在桌面快捷方式
- Android应用添加(创建)和删除及判断是否存在桌面快捷方式
- Android创建桌面快捷方式所遇到的问题与解决方案
- [置顶] android,解决手动创建的桌面快捷方式无法跳转到制定的activity的问题,提示未安装应用程序
- Android 判断桌面是否快捷方式,不存在则创建
- Android应用添加(创建)和删除及判断是否存在桌面快捷方式
- Android应用添加(创建)和删除及判断是否存在桌面快捷方式
- 桌面快捷方式的问题-创建-删除-判断
- Android创建桌面快捷方式和判断当前快捷方式是否已经存在
- Android应用添加(创建)和删除及判断是否存在桌面快捷方式
- Android应用添加(创建)和删除及判断是否存在桌面快捷方式
- Android应用添加(创建)和删除及判断是否存在桌面快捷方式
- android--创建快捷方式和判断是否已经创建
- 【Android开发总结(1)】桌面快捷方式创建、删除、查询等方式
- Android创建和删除桌面快捷方式
- [Android]为指定的应用创建桌面快捷方式
- Android创建和删除桌面快捷方式