首页 > 其他分享 >(转)Android快捷方式解密

(转)Android快捷方式解密

时间:2022-11-01 13:24:56浏览次数:90  
标签:String Launcher 解密 param Intent context 快捷方式 Android

Android快捷方式解密

泡在网上的日子 / 文 发表于2015-11-16 18:38 第次阅读   万维广告联盟 免费使用亚马逊云科技 30 余种核心云服务产品,包括云服务器,CDN等,长达 12 个月万维广告

 

原文出处:http://www.jianshu.com/p/dc3d04337d00 

Android快捷方式作为Android设备的杀手锏技能,一直都是非常重要的一个功能,也正是如此,各种流氓App也不断通过快捷方式霸占着这样一个用户入口。

同时,各大国产ROM和Luncher的崛起,让这个桌面之争变的更加激烈。毕竟大家都只想用户用自己的App资源,所以,现在各大App不仅仅是要抢占入口,同时还要和各大ROM斗智斗勇。本文将对这个快捷方式进行深度解密,同时给出App适配各种ROM的整合方案。

本文很多地方参考了这位朋友的实现:

https://gist.github.com/waylife/437a3d98a84f245b9582

特此表示感谢!

创建快捷方式之——少林派

所谓少林,是指系统正统的解决方法

天下武功出少林,天下的快捷方式都是Google给的,我们先来看看如何使用Android系统提供的方式来使用Android的快捷方式。

首先大家要知道各种Launcher的区别,原生的Launcher,是两层结构,桌面是快捷方式,而进去后的App列表是App的Launch Icon;而以小米为首的一帮ROM,参考iOS风格,将Launcher改为了一层,即直接显示Launch Icon。

权限设置

  1.     <!-- 添加快捷方式 -->
  2.     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
  3.     <!-- 移除快捷方式 -->
  4.     <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
  5.     <!-- 查询快捷方式 -->
  6.     <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

创建快捷方式

创建快捷方式的Action:

  1.   // Action 添加Shortcut
  2.     public static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

通过广播创建快捷方式:

  1.     /**
  2.      * 添加快捷方式
  3.      *
  4.      * @param context      context
  5.      * @param actionIntent 要启动的Intent
  6.      * @param name         name
  7.      */
  8.     public static void addShortcut(Context context, Intent actionIntent, String name,
  9.                                    boolean allowRepeat, Bitmap iconBitmap) {
  10.         Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT);
  11.         // 是否允许重复创建
  12.         addShortcutIntent.putExtra("duplicate", allowRepeat);
  13.         // 快捷方式的标题
  14.         addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
  15.         // 快捷方式的图标
  16.         addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmap);
  17.         // 快捷方式的动作
  18.         addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent);
  19.         context.sendBroadcast(addShortcutIntent);
  20.     }

参数相信大家都能看得懂,只是有一点需要注意的,duplicate这个属性,是设置该快捷方式是否允许多次创建的属性,但是,在很多ROM上都不能成功识别,嗯,这就是我们最开始说的快捷方式乱现象。

删除快捷方式

删除快捷方式的Action:

  1.  // Action 移除Shortcut
  2.     public static final String ACTION_REMOVE_SHORTCUT = "com.android.launcher.action.UNINSTALL_SHORTCUT";

通过广播删除快捷方式:

  1.     /**
  2.      * 移除快捷方式
  3.      *
  4.      * @param context      context
  5.      * @param actionIntent 要启动的Intent
  6.      * @param name         name
  7.      */
  8.     public static void removeShortcut(Context context, Intent actionIntent, String name) {
  9.         Intent intent = new Intent(ACTION_REMOVE_SHORTCUT);
  10.         intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
  11. //        intent.addCategory(Intent.CATEGORY_LAUNCHER);
  12.         intent.putExtra("duplicate", false);
  13.         intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent);
  14.         context.sendBroadcast(intent);
  15.     }

参数与创建快捷方式的方法击败类似,需要注意的是,Intent.EXTRA_SHORTCUT_INTENT,与之前创建快捷方式的Intent必须要是同一个,不然是无法删除快捷方式的。

创建快捷方式之——逍遥派

所谓逍遥派,是指我们从原理来理解如何来适配各种Launcher。

原生的快捷方式添加方法,虽然是官方提供的,但在天国这样一个怎么说呢的国家里,基本是很难使用、适配的,也就是我们最开始说的那些原因。下面我们先从快捷方式的整个生命周期来了解下产生、添加、删除快捷方式的原理,再来思考如何实现多ROM、Launcher的适配。

快捷方式的存储

快捷方式其实都存储在Launcher的数据库中,我们在手机上打开SQLite Editor打开Launcher的数据库。

blob.png

我们打开Launcher.db的favorite表,这里就是我们保存的快捷方式数据:

1447669201858484.png

几个主要的字段大家基本一看就懂:title、intent、iconResource、icon,分别对应快捷方式名称,快捷方式intent,快捷方式图标来源,快捷方式图标二进制数据。

快捷方式的创建

了解了快捷方式的存储原理,我们就可以针对这个数据库来做文章,所有的快捷方式都可以通过修改这个数据库来实现,同时还不用太考虑兼容性问题。

对于快捷方式的创建,我们依然可以使用系统提供的方法,所以这里不再多说。

快捷方式的判断是否存在

前面我们说了,通过duplicate属性可以区分是否允许创建重复的快捷方式,但是,很多ROM是无法兼容到的,所以,这里我们使用查询Launcher数据库的方式来实现。

我们先来看代码:

  1.     /**
  2.      * 检查快捷方式是否存在 <br/>
  3.      * <font color=red>注意:</font> 有些手机无法判断是否已经创建过快捷方式<br/>
  4.      * 因此,在创建快捷方式时,请添加<br/>
  5.      * shortcutIntent.putExtra("duplicate", false);// 不允许重复创建<br/>
  6.      * 最好使用{@link #isShortCutExist(Context, String, Intent)}
  7.      * 进行判断,因为可能有些应用生成的快捷方式名称是一样的的<br/>
  8.      */
  9.     public static boolean isShortCutExist(Context context, String title) {
  10.         boolean result = false;
  11.         try {
  12.             ContentResolver cr = context.getContentResolver();
  13.             Uri uri = getUriFromLauncher(context);
  14.             Cursor c = cr.query(uri, new String[]{"title"}, "title=? ", new String[]{title}, null);
  15.             if (c != null && c.getCount() > 0) {
  16.                 result = true;
  17.             }
  18.             if (c != null && !c.isClosed()) {
  19.                 c.close();
  20.             }
  21.         } catch (Exception e) {
  22.             result = false;
  23.             e.printStackTrace();
  24.         }
  25.         return result;
  26.     }
  27.     /**
  28.      * 不一定所有的手机都有效,因为国内大部分手机的桌面不是系统原生的<br/>
  29.      * 更多请参考{@link #isShortCutExist(Context, String)}<br/>
  30.      * 桌面有两种,系统桌面(ROM自带)与第三方桌面,一般只考虑系统自带<br/>
  31.      * 第三方桌面如果没有实现系统响应的方法是无法判断的,比如GO桌面<br/>
  32.      */
  33.     public static boolean isShortCutExist(Context context, String title, Intent intent) {
  34.         boolean result = false;
  35.         try {
  36.             ContentResolver cr = context.getContentResolver();
  37.             Uri uri = getUriFromLauncher(context);
  38.             Cursor c = cr.query(uri, new String[]{"title", "intent"}, "title=?  and intent=?",
  39.                     new String[]{title, intent.toUri(0)}, null);
  40.             if (c != null && c.getCount() > 0) {
  41.                 result = true;
  42.             }
  43.             if (c != null && !c.isClosed()) {
  44.                 c.close();
  45.             }
  46.         } catch (Exception ex) {
  47.             result = false;
  48.             ex.printStackTrace();
  49.         }
  50.         return result;
  51.     }
  52.     private static Uri getUriFromLauncher(Context context) {
  53.         StringBuilder uriStr = new StringBuilder();
  54.         String authority = LauncherUtil.getAuthorityFromPermissionDefault(context);
  55.         if (authority == null || authority.trim().equals("")) {
  56.             authority = LauncherUtil.getAuthorityFromPermission(context, LauncherUtil.getCurrentLauncherPackageName(context) + ".permission.READ_SETTINGS");
  57.         }
  58.         uriStr.append("content://");
  59.         if (TextUtils.isEmpty(authority)) {
  60.             int sdkInt = android.os.Build.VERSION.SDK_INT;
  61.             if (sdkInt < 8) { // Android 2.1.x(API 7)以及以下的
  62.                 uriStr.append("com.android.launcher.settings");
  63.             } else if (sdkInt < 19) {// Android 4.4以下
  64.                 uriStr.append("com.android.launcher2.settings");
  65.             } else {// 4.4以及以上
  66.                 uriStr.append("com.android.launcher3.settings");
  67.             }
  68.         } else {
  69.             uriStr.append(authority);
  70.         }
  71.         uriStr.append("/favorites?notify=true");
  72.         return Uri.parse(uriStr.toString());
  73.     }

这里有两个重载的isShortCutExist方法,唯一的区别就是最后一个参数——intent,加这个参数的原因,在注释中已经写了,更加精确。而getUriFromLauncher方法,是给调用的ContentResolver提供Uri。构造的时候,可以看见,Android的版本话碎片问题,是多么的严重……

这样在添加快捷方式前,通过这个判断下,就可以只添加一个快捷方式了。

为任意PackageName的App添加快捷方式

知道了我们是如何判断快捷方式是是否存在的,我们就可以通过这种思路来为任意PackageName的App添加快捷方式,代码如下:

  1.   /**
  2.      * 为PackageName的App添加快捷方式
  3.      *
  4.      * @param context context
  5.      * @param pkg     待添加快捷方式的应用包名
  6.      * @return 返回true为正常执行完毕
  7.      */
  8.     public static boolean addShortcutByPackageName(Context context, String pkg) {
  9.         // 快捷方式名
  10.         String title = "unknown";
  11.         // MainActivity完整名
  12.         String mainAct = null;
  13.         // 应用图标标识
  14.         int iconIdentifier = 0;
  15.         // 根据包名寻找MainActivity
  16.         PackageManager pkgMag = context.getPackageManager();
  17.         Intent queryIntent = new Intent(Intent.ACTION_MAIN, null);
  18.         queryIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 重要,添加后可以进入直接已经打开的页面
  19.         queryIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
  20.         queryIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
  21.         List<ResolveInfo> list = pkgMag.queryIntentActivities(queryIntent,
  22.                 PackageManager.GET_ACTIVITIES);
  23.         for (int i = 0; i < list.size(); i++) {
  24.             ResolveInfo info = list.get(i);
  25.             if (info.activityInfo.packageName.equals(pkg)) {
  26.                 title = info.loadLabel(pkgMag).toString();
  27.                 mainAct = info.activityInfo.name;
  28.                 iconIdentifier = info.activityInfo.applicationInfo.icon;
  29.                 break;
  30.             }
  31.         }
  32.         if (mainAct == null) {
  33.             // 没有启动类
  34.             return false;
  35.         }
  36.         Intent shortcut = new Intent(
  37.                 "com.android.launcher.action.INSTALL_SHORTCUT");
  38.         // 快捷方式的名称
  39.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
  40.         // 不允许重复创建
  41.         shortcut.putExtra("duplicate", false);
  42.         ComponentName comp = new ComponentName(pkg, mainAct);
  43.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
  44.                 queryIntent.setComponent(comp));
  45.         // 快捷方式的图标
  46.         Context pkgContext = null;
  47.         if (context.getPackageName().equals(pkg)) {
  48.             pkgContext = context;
  49.         } else {
  50.             // 创建第三方应用的上下文环境,为的是能够根据该应用的图标标识符寻找到图标文件。
  51.             try {
  52.                 pkgContext = context.createPackageContext(pkg,
  53.                         Context.CONTEXT_IGNORE_SECURITY
  54.                                 | Context.CONTEXT_INCLUDE_CODE);
  55.             } catch (PackageManager.NameNotFoundException e) {
  56.                 e.printStackTrace();
  57.             }
  58.         }
  59.         if (pkgContext != null) {
  60.             Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource
  61.                     .fromContext(pkgContext, iconIdentifier);
  62.             shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
  63.         }
  64.         // 发送广播,让接收者创建快捷方式
  65.         // 需权限<uses-permission
  66.         // android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
  67.         context.sendBroadcast(shortcut);
  68.         return true;
  69.     }

创建快捷方式之——星宿派

所谓星宿派,是指我们使用一些Trick来解决多Launcher适配的问题。

由于快捷方式的碎片化非常严重,所以,你顾得上这种ROM,顾不上其它ROM。例如,在原生ROM上,你需要使用类似原生的Launcher权限:

  1.     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
  2.     <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

但是,在其它ROM上呢,例如华为,你需要这样的权限:

  1.     <uses-permission android:name="com.huawei.launcher3.permission.READ_SETTINGS" />
  2.     <uses-permission android:name="com.huawei.launcher3.permission.WRITE_SETTINGS" />

为了程序能够通用性够强,理论上我们得为所有不使用原生Launcher权限的Launcher配置权限代码,是的,你妹听错,是所有,只有通过这种奇技淫巧,才能适配更多的Launcher,这里贴一部分给大家爽一下:

  1.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  2.     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  3.     <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
  4.     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
  5.     <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
  6.     <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
  7.     <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />
  8.     <uses-permission android:name="com.android.launcher2.permission.READ_SETTINGS" />
  9.     <uses-permission android:name="com.android.launcher2.permission.WRITE_SETTINGS" />
  10.     <uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS" />
  11.     <uses-permission android:name="com.android.launcher3.permission.WRITE_SETTINGS" />
  12.     <uses-permission android:name="org.adw.launcher.permission.READ_SETTINGS" />
  13.     <uses-permission android:name="org.adw.launcher.permission.WRITE_SETTINGS" />
  14.     <uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS" />
  15.     <uses-permission android:name="com.htc.launcher.permission.WRITE_SETTINGS" />
  16.     <uses-permission android:name="com.qihoo360.launcher.permission.READ_SETTINGS" />
  17.     <uses-permission android:name="com.qihoo360.launcher.permission.WRITE_SETTINGS" />
  18.     <uses-permission android:name="com.lge.launcher.permission.READ_SETTINGS" />
  19.     <uses-permission android:name="com.lge.launcher.permission.WRITE_SETTINGS" />
  20.     <uses-permission android:name="net.qihoo.launcher.permission.READ_SETTINGS" />
  21.     <uses-permission android:name="net.qihoo.launcher.permission.WRITE_SETTINGS" />
  22.     <uses-permission android:name="org.adwfreak.launcher.permission.READ_SETTINGS" />
  23.     <uses-permission android:name="org.adwfreak.launcher.permission.WRITE_SETTINGS" />
  24.     <uses-permission android:name="org.adw.launcher_donut.permission.READ_SETTINGS" />
  25.     <uses-permission android:name="org.adw.launcher_donut.permission.WRITE_SETTINGS" />
  26.     <uses-permission android:name="com.huawei.launcher3.permission.READ_SETTINGS" />
  27.     <uses-permission android:name="com.huawei.launcher3.permission.WRITE_SETTINGS" />
  28.     <uses-permission android:name="com.fede.launcher.permission.READ_SETTINGS" />
  29.     <uses-permission android:name="com.fede.launcher.permission.WRITE_SETTINGS" />
  30.     <uses-permission android:name="com.sec.android.app.twlauncher.settings.READ_SETTINGS" />
  31.     <uses-permission android:name="com.sec.android.app.twlauncher.settings.WRITE_SETTINGS" />
  32.     <uses-permission android:name="com.anddoes.launcher.permission.READ_SETTINGS" />
  33.     <uses-permission android:name="com.anddoes.launcher.permission.WRITE_SETTINGS" />
  34.     <uses-permission android:name="com.tencent.qqlauncher.permission.READ_SETTINGS" />
  35.     <uses-permission android:name="com.tencent.qqlauncher.permission.WRITE_SETTINGS" />
  36.     <uses-permission android:name="com.huawei.launcher2.permission.READ_SETTINGS" />
  37.     <uses-permission android:name="com.huawei.launcher2.permission.WRITE_SETTINGS" />
  38.     <uses-permission android:name="com.android.mylauncher.permission.READ_SETTINGS" />
  39.     <uses-permission android:name="com.android.mylauncher.permission.WRITE_SETTINGS" />
  40.     <uses-permission android:name="com.ebproductions.android.launcher.permission.READ_SETTINGS" />
  41.     <uses-permission android:name="com.ebproductions.android.launcher.permission.WRITE_SETTINGS" />
  42.     <uses-permission android:name="com.oppo.launcher.permission.READ_SETTINGS" />
  43.     <uses-permission android:name="com.oppo.launcher.permission.WRITE_SETTINGS" />
  44.     <uses-permission android:name="com.miui.mihome2.permission.READ_SETTINGS" />
  45.     <uses-permission android:name="com.miui.mihome2.permission.WRITE_SETTINGS" />
  46.     <uses-permission android:name="com.huawei.android.launcher.permission.READ_SETTINGS" />
  47.     <uses-permission android:name="com.huawei.android.launcher.permission.WRITE_SETTINGS" />
  48.     <uses-permission android:name="telecom.mdesk.permission.READ_SETTINGS" />
  49.     <uses-permission android:name="telecom.mdesk.permission.WRITE_SETTINGS" />
  50.     <uses-permission android:name="dianxin.permission.ACCESS_LAUNCHER_DATA" />

这时候大家肯定要问了,你申请这么多权限,用户在安装App的时候,不是要崩溃了,尼玛,这么多看都看不过来啊,其实,根本不需要担心,因为这些基本都是各自ROM中的第三方ROM权限,在用户安装的时候,他们通常会被解析成原生Launcher的权限,例如:添加、修改桌面快捷方式。并不会将所有的权限都写出来。

创建快捷方式之——西域派

所谓西域派,是因为我想不出其他名字了。西域一派,使用其他方式来实现类似快捷方式的方法。

快捷方式的确是我们为应用导流的一个非常重要的入口,但是,由于碎片化实在太严重,所以,我们可以使用在Launcher App列表中为应用增加一个入口的方式来为App导流,简单的说,就是增进一个App的入口Activity。

  1. <activity android:name=".MainActivity">
  2.     <intent-filter>
  3.         <action android:name="android.intent.action.MAIN" />
  4.         <category android:name="android.intent.category.LAUNCHER" />
  5.     </intent-filter>
  6. </activity>
  7. <activity
  8.     android:name="com.hujiang.hj_shortcut_lib.HJShortcutActivity"
  9.     android:theme="@style/Base.Theme.AppCompat.Dialog">
  10.     <intent-filter>
  11.         <action android:name="android.intent.action.MAIN" />
  12.         <category android:name="android.intent.category.LAUNCHER" />
  13.     </intent-filter>
  14. </activity>

非常简单,相信大家都知道这种方式来给App增加一个Activity入口。但是,这种方式,我们如何能够自由的控制这个入口是否显示呢?

奇技PackageManager

PackageManager提供了一系列Package的管理方法,当然,也包含了我们非常关心的启用、停用组件这一方法,这个方法在Root情况下,可以修改任一App的任意组件,在普通情况下,对自身App有绝对权限。使用方法也非常简单:

  1.     public static void toggleFlowEntrance(Context context, Class launcherClass) {
  2.         PackageManager packageManager = context.getPackageManager();
  3.         ComponentName componentName = new ComponentName(context, launcherClass);
  4.         int res = packageManager.getComponentEnabledSetting(componentName);
  5.         if (res == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT ||
  6.                 res == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
  7.             // 隐藏应用图标
  8.             packageManager.setComponentEnabledSetting(
  9.                     componentName,
  10.                     PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
  11.                     PackageManager.DONT_KILL_APP);
  12.         } else {
  13.             // 显示应用图标
  14.             packageManager.setComponentEnabledSetting(
  15.                     componentName,
  16.                     PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
  17.                     PackageManager.DONT_KILL_APP);
  18.         }
  19.     }

一统江湖

前面我们分析了各种快捷方式、Launcher入口的方式来对App进行导流,当然,这不是我们的目的,我们的目的是能够掌握Android快捷方式的哭花宝典而不用那个啥。

所以,下面我封装了一个shortcut的开源库,从而可以尽可能的忽略ROM的差异,来使用快捷方式和Launcher入口。

项目地址:

https://github.com/xuyisheng/ShortcutHelper

目前该项目还在测试阶段,还要很多问题和适配bug需要解决,欢迎大家提issue。

README如下:

ShortcutLib使用指南

本项目目前还在测试阶段,请大家多提issue,共同完善。

项目意义

快速使用shortcut,避免各种ROM适配导致的各种问题。

项目可用功能API

  • 增加快捷方式

    1.   /**
    2.      * 添加快捷方式
    3.      *
    4.      * @param context      context
    5.      * @param actionIntent 要启动的Intent
    6.      * @param name         name
    7.      * @param allowRepeat  是否允许重复
    8.      * @param iconBitmap   快捷方式图标
    9.      */
    10.     public static void addShortcut(Context context, Intent actionIntent, String name,
    11.                                    boolean allowRepeat, Bitmap iconBitmap)
  • 判断快捷方式是否存在

    基础方式

    1.     /**
    2.      * 判断快捷方式是否存在
    3.      * <p/>
    4.      * 检查快捷方式是否存在 <br/>
    5.      * <font color=red>注意:</font> 有些手机无法判断是否已经创建过快捷方式<br/>
    6.      * 因此,在创建快捷方式时,请添加<br/>
    7.      * shortcutIntent.putExtra("duplicate", false);// 不允许重复创建<br/>
    8.      * 最好使用{@link #isShortCutExist(Context, String, Intent)}
    9.      * 进行判断,因为可能有些应用生成的快捷方式名称是一样的的<br/>
    10.      *
    11.      * @param context context
    12.      * @param title   快捷方式名
    13.      * @return 是否存在
    14.      */
    15.     public static boolean isShortCutExist(Context context, String title)

    严格方式(增加Intent的检查)

    1.     /**
    2.      * 判断快捷方式是否存在
    3.      * <p/>
    4.      * 不一定所有的手机都有效,因为国内大部分手机的桌面不是系统原生的<br/>
    5.      * 更多请参考{@link #isShortCutExist(Context, String)}<br/>
    6.      * 桌面有两种,系统桌面(ROM自带)与第三方桌面,一般只考虑系统自带<br/>
    7.      * 第三方桌面如果没有实现系统响应的方法是无法判断的,比如GO桌面<br/>
    8.      *
    9.      * @param context context
    10.      * @param title   快捷方式名
    11.      * @param intent  快捷方式Intent
    12.      * @return 是否存在
    13.      */
    14.     public static boolean isShortCutExist(Context context, String title, Intent intent

更新快捷方式

  1.    /**
  2.      * 更新桌面快捷方式图标,不一定所有图标都有效(有可能需要系统权限)
  3.      *
  4.      * @param context context
  5.      * @param title   快捷方式名
  6.      * @param intent  快捷方式Intent
  7.      * @param bitmap  快捷方式Icon
  8.      */
  9.     public static void updateShortcutIcon(Context context, String title, Intent intent, Bitmap bitmap)

需要注意的是,更新快捷方式在很多手机上都不能生效,需要系统权限。可以通过先删除、再新增的方式来实现。

为任意PackageName的App添加快捷方式

  1.    /**
  2.      * 为任意PackageName的App添加快捷方式
  3.      *
  4.      * @param context context
  5.      * @param pkg     待添加快捷方式的应用包名
  6.      * @return 返回true为正常执行完毕
  7.      */
  8.     public static boolean addShortcutByPackageName(Context context, String pkg)

移除快捷方式

  1.     /**
  2.      * 移除快捷方式
  3.      *
  4.      * @param context      context
  5.      * @param actionIntent 要启动的Intent
  6.      * @param name         name
  7.      */
  8.     public static void removeShortcut(Context context, Intent actionIntent, String name)

显示隐藏Launcher入口

  1.     /**
  2.      * 显示\隐藏Launcher入口
  3.      *
  4.      * @param context       context
  5.      * @param launcherClass launcherClass
  6.      */
  7.     public static void toggleFlowEntrance(Context context, Class launcherClass)

使用Launcher入口需要在AndroidMainifest文件中注册新增的入口Activity,例如:

  1. <activity android:name=".MainActivity">
  2.     <intent-filter>
  3.         <action android:name="android.intent.action.MAIN" />
  4.         <category android:name="android.intent.category.LAUNCHER" />
  5.     </intent-filter>
  6. </activity>
  7. <activity
  8.     android:name="com.xxx.xxxxx"
  9.     android:theme="@style/Base.Theme.AppCompat.Dialog">
  10.     <intent-filter>
  11.         <action android:name="android.intent.action.MAIN" />
  12.         <category android:name="android.intent.category.LAUNCHER" />
  13.     </intent-filter>
  14. </activity>

使用示例

  1.     public void addShortcutTest(View view) {
  2.         // 系统方式创建
  3.         // ShortcutUtils.addShortcut(this, getShortCutIntent(), mShortcutName);
  4.         // 创建前判断是否存在
  5.         if (!ShortcutSuperUtils.isShortCutExist(this, mShortcutName, getShortCutIntent())) {
  6.             ShortcutUtils.addShortcut(this, getShortCutIntent(), mShortcutName, false,
  7.                     BitmapFactory.decodeResource(getResources(), com.hujiang.hj_shortcut_lib.R.drawable.ocsplayer));
  8.             finish();
  9.         } else {
  10.             Toast.makeText(this, "Shortcut is exist!", Toast.LENGTH_SHORT).show();
  11.         }
  12.         // 为某个包创建快捷方式
  13.         // ShortcutSuperUtils.addShortcutByPackageName(this, this.getPackageName());
  14.     }
  15.     public void removeShortcutTest(View view) {
  16.         ShortcutUtils.removeShortcut(this, getShortCutIntent(), mShortcutName);
  17.     }
  18.     public void updateShortcutTest(View view) {
  19.         ShortcutSuperUtils.updateShortcutIcon(this, mShortcutName, getShortCutIntent(),
  20.                 BitmapFactory.decodeResource(getResources(), com.hujiang.hj_shortcut_lib.R.mipmap.ic_launcher));
  21.     }
  22.     public void toggleFlowEntrance(View view) {
  23.         FlowEntranceUtil.toggleFlowEntrance(this, HJShortcutActivity.class);
  24.     }
  25.     private Intent getShortCutIntent() {
  26.         // 使用MAIN,可以避免部分手机(比如华为、HTC部分机型)删除应用时无法删除快捷方式的问题
  27.         Intent intent = new Intent(Intent.ACTION_MAIN);
  28.         intent.addCategory(Intent.CATEGORY_DEFAULT);
  29.         intent.setClass(MainActivity.this, HJShortcutActivity.class);
  30.         return intent;
  31.     }

 

标签:String,Launcher,解密,param,Intent,context,快捷方式,Android
From: https://www.cnblogs.com/ioriwellings/p/16198693.html

相关文章