Android创建桌面快捷方式

首先创建、删除快捷方式需要权限:
  1. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />  
  2. <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />  

推荐方案:

这个是我自己使用的方案,下面其他的是备选方案,万一这个有问题的,可以结合下面的方案做一下调整。
删除快捷方式可以根据需要取舍,Android的快捷方式一旦创建,如果新版本更新App图标,在覆盖安装的情况下是不会将之前创建的快捷方式图标更换成新图标的,除非手动删除再创建新的快捷方式,或者卸载App,否则这个快捷方式的图标是不会改变的,当需要保证图标是最新的又不需要用户手动操作,删除快捷方式的方法就派上用场了。
public class Utils {
	
	/**
	 * 为程序创建桌面快捷方式
	 * 与delShortcut是一套
	 * @param context
	 * @param clazz  启动Activity
	 */
	public static void addShortcut(Context context,Class<? extends Activity> clazz) {
		//此处是为了判断是否已经有了快捷方式
		if(Utils.hasShortCut(context)){
			return;
		}
		
		Intent shortcut = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");

		// 快捷方式的名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				context.getString(R.string.app_name));
		shortcut.putExtra("duplicate", false); // 不允许重复创建

		
		Intent intent = new Intent(context, clazz);
		intent.setAction("android.intent.action.MAIN");
		intent.addCategory("android.intent.category.LAUNCHER");
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

		// 快捷方式的图标
		ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
				context, R.drawable.ic_launcher);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

		context.sendBroadcast(shortcut);
	}

	/**
	 * 删除程序的快捷方式
	 * @param context
	 * @param clazz   启动Activity
	 */
	public static void delShortcut(Context context,Class<? extends Activity> clazz) {
		Intent shortcut = new Intent(
				"com.android.launcher.action.UNINSTALL_SHORTCUT");

		// 快捷方式的名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				context.getString(R.string.app_name));

		Intent intent = new Intent(context, clazz);
		intent.setAction("android.intent.action.MAIN");
		intent.addCategory("android.intent.category.LAUNCHER");

		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);

		context.sendBroadcast(shortcut);

	}
	
	
	

	
	/**
	 * 判断当前应用在桌面是否有桌面快捷方式
	 * 
	 * @param context
	 */
	public static boolean hasShortCut(Context context) {
		try {
			Uri uri = null;
			String spermi = getAuthorityFromPermission(context,
					"com.android.launcher.permission.READ_SETTINGS");
			if (spermi == null) {
				return false;
			}
			if (getSystemVersion() < 8) {
				uri = Uri.parse("content://" + spermi
						+ "/favorites?notify=true");
			} else {
				uri = Uri.parse("content://" + spermi
						+ "/favorites?notify=true");
			}
			final ContentResolver cr = context.getContentResolver();
			Cursor cursor = cr.query(uri, null, "title=?", new String[] { context
					.getResources().getString(R.string.app_name) }, null);
			if (cursor != null && cursor.getCount() > 0) {
				cursor.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return hasShortcut(context);
		}

	}

	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 (provider.readPermission != null) {
							if ((provider.readPermission).equals(permission)) {
								return provider.authority;
							}
						}
					}
				}
			}
		}
		return null;

	}

	public static int getSystemVersion() {
		return android.os.Build.VERSION.SDK_INT;
	}

	/**
	 * 当读取桌面快捷方式权限有问题时,执行该方法判断
	 * @param context
	 * @return
	 */
	private static boolean hasShortcut(Context context) {
		try {
			boolean isInstallShortcut = false;
			Uri uri = null;
			if (getSystemVersion() < 8) {
				uri = Uri
						.parse("content://com.android.launcher.settings/favorites?notify=true");
			} else {
				uri = Uri
						.parse("content://com.android.launcher2.settings/favorites?notify=true");
			}
			Cursor c = context.getContentResolver().query(uri,
					new String[] { "title", "iconResource" }, "title=?",
					new String[] { context.getString(R.string.app_name).trim() },
					null);
			if (null != c && c.getCount() > 0) {
				isInstallShortcut = true;
			}

			return isInstallShortcut;
		} catch (Exception e) {
			return true;
		}

	}
	
}

备选方案:

/**
  * 为程序创建桌面快捷方式
  */
 private void addShortcut(){
 
  Intent intent = new Intent();
  intent.setAction("android.intent.action.MAIN");//使快捷方式和应用程序关联,卸载时可以一起卸载掉
  intent.setClass(this.getApplicationContext(), StartActivity.class);//表明程序启动的activity
 
  shortcut= new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
  //快捷方式的名称
  shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
  shortcut.putExtra("duplicate", false); //不允许重复创建
  shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
  //快捷方式的图标
  ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
  shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
  sendBroadcast(shortcut);
 }
 /**
  * 删除程序的快捷方式
  */
 private void delShortcut(){
  Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
   
  //快捷方式的名称
  shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
   
  //指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
  //注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式
  String appClass = this.getPackageName() + "." +this.getLocalClassName();
  ComponentName comp = new ComponentName(this.getPackageName(), appClass);
  shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
   
  sendBroadcast(shortcut);
   
 }



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值