Android判断快捷方式是否存在

每天在奋斗的IT界的人们,你们好!上次在项目中有个需求是对APP创建快捷方式,既然是创建快捷方式,那么肯定是需要判断当前APP的快捷方式是否已经有创建,如果有创建那就不再重新创建了,否则,用户会干死我们的。

说实话,上次找了好多资料,都没有实现,不是说没有实现吧,是没有找到一个兼容的方法,有个华为的U8080的手机,怎么也不行。。(最后我也没解决)

下面共享一个总结后的方法:对大部分的手机都是可以的,上次就U8080这个手机没有实现,其他都OK(当然我发这个贴,第一:是为了记住这个的一个方法,第二:如有有哪位朋友有好的解决方案,还请分享一下)

package com.teebik.push.iconpush;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ProviderInfo;
import android.database.Cursor;
import android.text.TextUtils;

public class IconPushHandler {	<span style="font-family: Arial, Helvetica, sans-serif;">		</span>
	public void createIconPush() {
		if (!hasShortCut() && !hasShortcut(context)) {  //如果两个方法判断的结果同时成立时,创建快捷方式
			createSystemSwitcherShortCut(context);
		}
	}

	public void createSystemSwitcherShortCut(Context context) {  <span style="font-family: Arial, Helvetica, sans-serif;">//创建快捷键</span>
		Intent addIntent = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");
		addIntent.putExtra("duplicate", false);

		Intent myIntent = new Intent(context, TeebikIconPushActivity.class);  //<span style="font-family: Arial, Helvetica, sans-serif;">TeebikIconPushActivity代表点击快捷方式启动的Activity</span>
		myIntent.setAction("android.intent.action.MAIN");
		myIntent.addCategory("android.intent.category.LAUNCHER");
		myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, R.string.app_name);
		addIntent.putExtra("android.intent.extra.shortcut.ICON", R.drawble.icon);
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);
		context.sendBroadcast(addIntent);
	}

	private boolean hasShortCut() {    //判断快捷键是否存在 方法一
		ContentResolver resolver = context.getContentResolver();
		Cursor cursor = resolver
				.query(Uri
						.parse("content://com.android.launcher2.settings/favorites?notify=true"),
						null, "title=?", new String[] { R.string.app_name}, null);

		if (cursor != null && cursor.moveToFirst()) {
			cursor.close();
			return true;
		}

		return false;
	}

	private  boolean hasShortcut(Context context) {   <span style="font-family: Arial, Helvetica, sans-serif;">//判断快捷键是否存在 方法二</span>
		String AUTHORITY = getAuthorityFromPermission(context,
				"com.android.launcher.permission.READ_SETTINGS");
		System.out.println(" AUTHORITY ..." + AUTHORITY);
		if (AUTHORITY == null) {
			return false;
		}
		Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
				+ "/favorites?notify=true");
		String title = "";
		final PackageManager packageManager = context.getPackageManager();
		try {
			title = packageManager.getApplicationLabel(
					packageManager.getApplicationInfo(context.getPackageName(),
							PackageManager.GET_META_DATA)).toString();
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		Cursor c = context.getContentResolver()
				.query(CONTENT_URI, new String[] { "title" }, "title=?",
						new String[] {R.string.app_name}, null);
		if (c != null && c.getCount() > 0) {
			return true;
		}
		return false;
	}

	private static String getAuthorityFromPermission(Context context,
			String permission) {
		if (TextUtils.isEmpty(permission)) {
			return null;
		}
		List<PackageInfo> packageInfoList = context.getPackageManager()
				.getInstalledPackages(PackageManager.GET_PROVIDERS);
		if (packageInfoList == null) {
			return null;
		}
		for (PackageInfo packageInfo : packageInfoList) {
			ProviderInfo[] providerInfos = packageInfo.providers;
			if (providerInfos != null) {
				for (ProviderInfo providerInfo : providerInfos) {
					if (permission.equals(providerInfo.readPermission)
							|| permission.equals(providerInfo.writePermission)) {
						return providerInfo.authority;
					}
				}
			}
		}
		return null;
	}
}

(学习是分享与探讨的过程,希望大家一起)

如有更好的方法请分享,谢谢

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值