Android中创建和检测快捷方式
2011-04-14 18:19
405 查看
public class ShortcutUtil { /** * 创建快捷方式 * * @param context */ public static void createShortcut(Context context, Class<?> target) { // <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon)); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name)); intent.putExtra("duplicate", false); Intent sIntent = new Intent(Intent.ACTION_MAIN); sIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 加入action,和category之后,程序卸载的时候才会主动将该快捷方式也卸载 sIntent.setClass(context, target); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, sIntent); context.sendBroadcast(intent); } /** * 判断桌面上是否有的快捷方式 * * @return */ public static boolean hasShortcut(Context context) { final ContentResolver cr = context.getContentResolver(); final String AUTHORITY = "com.android.launcher.settings"; final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); // <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/> Cursor c = cr.query(CONTENT_URI, new String[] { "intent" }, null, null, null); if (c == null) { // 注: 2.1update和2.2版本的真机上测试无法访问com.android.launcher.settings,2.1update1的模拟器上可以 // ERROR/ActivityThread(1136): Failed to find provider info for com.android.launcher.settings return false; } while (c.moveToNext()) { String intentstring = c.getString(c.getColumnIndex("intent")); if (intentstring == null) { continue; } String componentString = getComponentString(intentstring); if (componentString.startsWith(context.getPackageName())) { return true; } } return false; } private static String getComponentString(String intentInfo) { // intent info 的格式:intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.allstar.tanzhi/.activities.StartActivity;end int start = intentInfo.indexOf("component") + 9 + 1; int end = intentInfo.indexOf(";", start); return intentInfo.substring(start, end); }
相关文章推荐
- Android 为应用创建、删除桌面快捷方式
- Android 为应用程序中的任意Activity创建桌面快捷方式
- Android程序开发之手机APP创建桌面快捷方式
- android开发步步为营之49:创建APP桌面快捷方式
- Android创建桌面快捷方式
- Android创建快捷方式
- android快捷方式图标的创建
- Android学习之程序创建桌面快捷方式
- Android — 创建快捷方式
- Android中为APP创建快捷方式的原理(自己的理解)
- Android在桌面创建快捷方式
- Android 创建和删除快捷方式以及判断是否有快捷方式
- Android 创建桌面快捷方式
- Android 判断、创建和删除快捷方式
- 总结:android 创建快捷方式的两种方式+判断是否已经创建+删除快捷方式
- Android创建和删除桌面快捷方式
- Android创建桌面快捷方式并像启动Activity传递参数
- Android应用添加(创建)和删除及判断是否存在桌面快捷方式
- Android应用开发之(如何自动在桌面创建快捷方式)
- Android创建桌面快捷方式两种方法