ANDROID 桌面快捷方式创建,和判断 返回false问题

创建快捷方式

/**
	 * 创建快捷方式
	 */
	public static void addShortcut(Activity context,Class calss)
	{
		if(isAddShortCut(context))
			return ;
		Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
		// 设置属性
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context. getResources().getString(R.string.app_name));
		ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context.getApplicationContext(), R.drawable.ic_launcher);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);

		// 是否允许重复创建
		shortcut.putExtra("duplicate", false);

		//设置桌面快捷方式的图标
		Parcelable icon = Intent.ShortcutIconResource.fromContext(context,R.drawable.ic_launcher);        
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);

		//点击快捷方式的操作
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		intent.setClass(context,calss);

		// 设置启动程序
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

		//广播通知桌面去创建
		context.sendBroadcast(shortcut);
	}

一般要判断下是否已经创建了

public static boolean isAddShortCut(Activity context) {
		String url = "";
		try {
			url = "content://" + getAuthorityFromPermission(context, "com.android.launcher.permission.READ_SETTINGS")            + "/favorites?notify=true";
			ContentResolver resolver = context.getContentResolver();
			Cursor cursor = resolver.query(Uri.parse(url), new String[] { "title", "iconResource" }, "title=?", new String[] { context.getString(R.string.app_name).trim() }, null);
			if (cursor != null && cursor.moveToFirst()) {
				cursor.close();
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


因为不同的手机厂商可能对手机系统进行了修改使用原生的

“content://com.android.launcher.settings/favorites?notify=true"

或者

"content://com.android.launcher2.settings/favorites?notify=true"

并不能准确判断 需要通过权限去获取当前手机provider.authority


/**
	 * 通过权限去获取当前手机provider.authority
	 * @param context
	 * @param permission
	 * @return
	 */
	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 (permission.equals(provider.readPermission))
							return provider.authority;
						if (permission.equals(provider.writePermission))
							return provider.authority;
					}
				}
			}
		}
		return null;
	}

权限配置,要在创建快捷方式的activity加  <action android:name="android.intent.action.CREATE_SHORTCUT"></action>

<activity
            android:name="com.hw.weather.fuzhou.LaunchActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
             <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT"></action>
            </intent-filter>
        </activity>

 <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <!-- 创建删除快捷方式 -->
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
    <!-- 创建删除快捷方式  MOTO的部分机型还要加上以下权限 -->
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值