文章来源:http://www.itnose.net/detail/6040401.html

更多文章:http://www.itnose.net/type/85.html

android判断和创建快捷方式(4.03测试通过)

整理了网上的创建方式的代码,对于快捷方式的判断使用系统api获取当前启动器来处理,这样系统定制过或者启动器不一样也没关系 。

一加权限和声明目标activity

    <!-- 创建快捷方式 -->    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

  <activity            android:name="com.shortcut.TestActivity"            android:configChanges="keyboardHidden|orientation"            android:theme="@android:style/Theme.Translucent" >            <intent-filter>                <action android:name="action.com.shortcut.test" />            </intent-filter>        </activity>

二创建代码

/** * 创建快捷方式 *  * @param context * @param name 显示名称 * @param url  */public static void createShortCut(Context context, String name, String url) {if (hasShortCut(context, name)) {        Log.v("createShortCut", name + "快捷方式已存在");return;}final Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");shortcutIntent.putExtra("duplicate", false);final Parcelable icon = Intent.ShortcutIconResource.fromContext(context,ResourceUtil.getId(context, "drawable", "o2o_game_float_icon"));// 这个参数是启动的activity的actionfinal Intent targetIntent = new Intent("action.com.shortcut.test");// 目标activitytargetIntent.setClassName(context.getPackageName(),"com.shortcut.TestActivity");targetIntent.putExtra("url", url);targetIntent.putExtra("name", name);targetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);context.sendBroadcast(shortcutIntent);}/** * 判断快捷方式是否已存在 *  * @param context * @param name * @return */private static boolean hasShortCut(Context context, String name) {Log.v("LauncherPackageName",getLauncherPackageName(context));String launcherPackage = getLauncherPackageName(context);if (TextUtils.isEmpty(launcherPackage)) {// 查询不到启动器时默认已存在快捷方式,不进行创建return true;}// Log.v("LauncherPackageName", launcherPackage);boolean result = false;final String uriStr = "content://" + launcherPackage+ ".settings/favorites?notify=true";final Uri CONTENT_URI = Uri.parse(uriStr);final Cursor c = context.getContentResolver().query(CONTENT_URI, null,"title=?", new String[] { name }, null);if (c != null && c.getCount() > 0) {result = true;}return result;}/** * 获取正在运行桌面包名(注:存在多个桌面时且未指定默认桌面时,该方法返回"",使用时需处理这个情况) */private static String getLauncherPackageName(Context context) {final Intent intent = new Intent(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_HOME);final ResolveInfo res = context.getPackageManager().resolveActivity(intent, 0);if (res.activityInfo == null) {// should not happen. A home is always installed, isn't it?return "";}if (res.activityInfo.packageName.equals("android")) {// 有多个桌面程序存在,且未指定默认项时;return "";} else {return res.activityInfo.packageName;}}

更多相关文章

  1. Android应用添加(创建)和删除及判断是否存在桌面快捷方式
  2. android安装运行launcher并修改系统内置桌面图标
  3. Android 快速学习onTouch,并完成实验单点滑动,双点触控,多点触控。(
  4. 多种方式实现动态替换Android默认桌面Launcher
  5. android——桌面组件的开发
  6. Android 桌面组件【widget】初探
  7. Android launcher 桌面抽屉切换动画
  8. Android 桌面组件【app widget】 进阶项目--心情记录器

随机推荐

  1. 30个高质量并且免费的Android图标【Andro
  2. Android高手进阶教程(四)之----Android(
  3. Android高手进阶教程(四)之----Android(
  4. Android内核与主线linux内核的比较(Androi
  5. android仿网易云音乐引导页、仿书旗小说F
  6. Android输入事件从读取到分发一:是谁在读
  7. 站在巨人的肩膀上学习Android开发
  8. [转]Google Android手机应用开发环境的搭
  9. Matrix详解
  10. android集成 任务调度 cron4j