`

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

 
阅读更多

文章来源: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的action
		final Intent targetIntent = new Intent(
				"action.com.shortcut.test");

		// 目标activity
		targetIntent.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;
		}
	}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics