Android应用添加(创建)和删除及判断是否存在桌面快捷方式

摘要:
Android桌面程序提供了添加和删除桌面快捷方式以及确定快捷方式是否存在的功能。您只需输入快捷方式标题、图标,然后单击快捷方式执行的应用程序Intent。

Android桌面程序提供了应用添加和删除桌面快捷方式的功能以及判断快捷方式是否存在,只要传入快捷方式标题、图标及点击快捷方式执行的应用Intent即可。代码如下:

1、Android添加桌面快捷方式

/**
 * 为当前应用添加桌面快捷方式
 * 
 * @param cx
 * @param appName
 *            快捷方式名称
 */
public static void addShortcut(Context cx) {
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

    Intent shortcutIntent = cx.getPackageManager()
            .getLaunchIntentForPackage(cx.getPackageName());
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // 获取当前应用名称
    String title = null;
    try {
        final PackageManager pm = cx.getPackageManager();
        title = pm.getApplicationLabel(
                pm.getApplicationInfo(cx.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(cx,
            R.drawable.ic_launcher);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

    cx.sendBroadcast(shortcut);
}

2、Android删除桌面快捷方式

/**
 * 删除当前应用的桌面快捷方式
 * 
 * @param cx
 */
public static void delShortcut(Context cx) {
    Intent shortcut = new Intent(
            "com.android.launcher.action.UNINSTALL_SHORTCUT");

    // 获取当前应用名称
    String title = null;
    try {
        final PackageManager pm = cx.getPackageManager();
        title = pm.getApplicationLabel(
                pm.getApplicationInfo(cx.getPackageName(),
                        PackageManager.GET_META_DATA)).toString();
    } catch (Exception e) {
    }
    // 快捷方式名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
    Intent shortcutIntent = cx.getPackageManager()
            .getLaunchIntentForPackage(cx.getPackageName());
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    cx.sendBroadcast(shortcut);
}

3、Android判断应用是否已存在桌面快捷方式

/**
 * 判断桌面是否已添加快捷方式
 * 
 * @param cx
 * @param titleName
 *            快捷方式名称
 * @return
 */
public static boolean hasShortcut(Context cx) {
    boolean result = false;
    // 获取当前应用名称
    String title = null;
    try {
        final PackageManager pm = cx.getPackageManager();
        title = pm.getApplicationLabel(
                pm.getApplicationInfo(cx.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 {
        uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
    }
    final Uri CONTENT_URI = Uri.parse(uriStr);
    final Cursor c = cx.getContentResolver().query(CONTENT_URI, null,
            "title=?", new String[] { title }, null);
    if (c != null && c.getCount() > 0) {
        result = true;
    }
    return result;
}

4、相关权限配置

<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" />

原文:http://orgcent.com/android-add-del-shortcut-desktop/ | 萝卜白菜的博客

免责声明:文章转载自《Android应用添加(创建)和删除及判断是否存在桌面快捷方式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇oracle常用的快捷键第三篇 基于.net搭建热插拔式web框架(重造Controller)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android中常见的MVC模式

MVC模式的简要介绍 MVC是三个单词的缩写,分别为: 模型(Model),视图(View)和控制Controller)。 MVC模式的目的就是实现Web系统的职能分工。 Model层实现系统中的业务逻辑。 View层用于与用户的交互。 Controller层是Model与View之间沟通的桥梁,它可以分派用户的请求并选择恰当的视图以用于显示,同时它也可以...

局域网永恒之蓝病毒发包的解决方案之二

1. 使用shadowbroker 的 eternalblue还有 doublepulsar 实现注入到目标机器的进程, 然后使用kali下的msf 获取到有病毒机器的shell 然后修改密码 查看问题等. 根据网上的教程来进行处理 以及复现等 教程地址: http://blog.csdn.net/claygrit/article/details/7728...

如何在Java应用中提交Spark任务?

最近看到有几个Github友关注了Streaming的监控工程——Teddy,所以思来想去还是优化下代码,不能让别人看笑话啊。于是就想改一下之前觉得最丑陋的一个地方——任务提交。 本博客内容基于Spark2.2版本~在阅读文章并想实际操作前,请确保你有: 一台配置好Spark和yarn的服务器 支持正常spark-submit --master yarn...

指纹解锁亮屏时间 Log 分析

极力推荐Android 开发大总结文章:欢迎收藏Android 开发技术文章大总结 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容: 1.解锁指纹,发送指纹中断Log 2.认证指纹,计算指纹耗时 3.指纹认证成功,keyguard 响应解锁屏流程 4.keyguard 解锁结束,开启动Launcher至绘制...

30个高质量并且免费的Android图标【Android Icon素材】

有 时候你可能不喜欢默认的应用程序图标,取而代之的是一些个性化的图标。实际上对于Android设备的用户来说,改变桌面图标是可能的。你仅仅需 要一些应用程序比如LauncherPro,GO Launcher EX, Apex Launcher, Nova Launcher等来帮你完成图标的替换。网上有很多的ICON可以供你选择和挑选,但是它们过于零散,找...

解决The goal you specified requires a project to execute but there is no POM in this directory错误的一种方法!

解决The goal you specified requires a project to execute but there is no POM in this directory错误的一种方法! 在idea工作空间没有pom 我看了下我的项目确实没有再我指定的工作空间  抛异常咯 把该项目移进去就解决了 来源;https://blog.csdn.ne...