Android判断快捷方式是否存在

来源:互联网 发布:淘宝为什么不能买彩票 编辑:程序博客网 时间:2024/05/29 15:23

每天在奋斗的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
原创粉丝点击