首页 技术 正文
技术 2022年11月15日
0 收藏 401 点赞 2,297 浏览 3071 个字

思路:

  Launcher为了应用程序能够定制自己的快捷图标,就注册了一个 BroadcastReceiver 专门接收其他应用程序发来的快捷图标定制信息。所以只需要根据该 BroadcastReceiver 构造出相对应的Intent并装入我们的定制信息,最后调用 sendBroadcast 方法就可以创建一个快捷图标了。

步骤:

  1. 创建快捷方式必须要有权限;
  2. 创建快捷方式的广播的 Intent 的 action 设置 com.android.launcher.action.INSTALL_SHORTCUT
  3. 删除快捷方式的广播的 Intent 的 action 设置 com.android.launcher.action.UNINSTALL_SHORTCUT
  4. 设置快捷方式的图片和名称等信息放在 Intent 中;

  需要添加的权限如下:

   <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher2.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS"/>

  核心代码为:

   /**
* 添加当前应用的桌面快捷方式
*
* @param context
*/
public static void addShortcut(Context context, int appIcon) {
Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT"); Intent shortcutIntent = context.getPackageManager()
.getLaunchIntentForPackage(context.getPackageName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// 获取当前应用名称
String title = null;
try {
final PackageManager pm = context.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
}
// 快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
// 不允许重复创建(不一定有效)
shortcut.putExtra("duplicate", false);
// 快捷方式的图标
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(context,
appIcon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); context.sendBroadcast(shortcut);
} /**
* 删除当前应用的桌面快捷方式
*
* @param context
*/
public static void delShortcut(Context context) {
Intent shortcut = new Intent(
"com.android.launcher.action.UNINSTALL_SHORTCUT"); // 获取当前应用名称
String title = null;
try {
final PackageManager pm = context.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
}
// 快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
Intent shortcutIntent = context.getPackageManager()
.getLaunchIntentForPackage(context.getPackageName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
context.sendBroadcast(shortcut);
} /**
* 判断当前应用在桌面是否有桌面快捷方式
*
* @param context
*/
public static boolean hasShortcut(Context context) {
boolean result = false;
String title = null;
try {
final PackageManager pm = context.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) { } final String uriStr;
if (android.os.Build.VERSION.SDK_INT < 8) {
uriStr = "content://com.android.launcher.settings/favorites?notify=true";
} else if (android.os.Build.VERSION.SDK_INT < 19) {
uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
} else {
uriStr = "content://com.android.launcher3.settings/favorites?notify=true";
}
final Uri CONTENT_URI = Uri.parse(uriStr);
final Cursor c = context.getContentResolver().query(CONTENT_URI, null,
"title=?", new String[]{title}, null);
if (c != null && c.getCount() > 0) {
result = true;
}
return result;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902