您的位置:首页 > 移动开发 > Android开发

Android创建快捷方式实现

2014-07-29 18:51 363 查看
/**
	 * create icon on desktop
	 */
	private void createShortcutIcon() {
		boolean isAdded = isInstallShortcut();
		boolean cIsAdded = ConfigMng.getInstance().loadBooleanKey(ConfigMng.APP_SHORT_CUT_ADDED, false);
		if (isAdded || cIsAdded) {
			return;
		}

		Intent shortcutIntent = new Intent(getApplicationContext(), LoadingActivity.class.getClass());
		shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		shortcutIntent.setAction("android.intent.action.MAIN");
		shortcutIntent.addCategory("android.intent.category.LAUNCHER");

		Intent addIntent = new Intent();
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
				Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon));
		addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
		getApplicationContext().sendBroadcast(addIntent);

		// save status
		ConfigMng.getInstance().saveBooleanKey(ConfigMng.APP_SHORT_CUT_ADDED, true);
		ConfigMng.getInstance().commit();
	}

	private boolean isInstallShortcut() {
		boolean isInstallShortcut = false;
		final ContentResolver cr = getContentResolver();
		final String AUTHORITY = "com.android.launcher.settings";
		final String AUTHORITY2 = "com.android.launcher2.settings";
		final Uri CONTENT_URI = Uri.parse("content://" +
				AUTHORITY + "/favorites?notify=true");
		final Uri CONTENT_URI2 = Uri.parse("content://" +
				AUTHORITY2 + "/favorites?notify=true");
		Cursor c = cr.query(CONTENT_URI2,
				new String[] { "title", "iconResource" },
				"title=?", new String[] { getString(R.string.app_name) }, null);
		if (c == null) {
			c = cr.query(CONTENT_URI,
					new String[] { "title", "iconResource" },
					"title=?", new String[] { getString(R.string.app_name) }, null);
		}

		if (c != null && c.getCount() > 0) {
			isInstallShortcut = true;
		}
		return isInstallShortcut;
	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: