Android添加快捷方式(Short)到手机桌面

权限

  要在手机桌面上添加快捷方式,首先需要在manifest中添加权限。

    <!-- 添加快捷方式 -->
<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" />

添加快捷方式

  添加快捷方式,是向桌面应用(launcher)发送相关action的广播,相关的action如下:

  public static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

  添加快捷方式:

    private void addShortcut(String name) {
Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT); // 不允许重复创建
addShortcutIntent.putExtra("duplicate", false);// 经测试不是根据快捷方式的名字判断重复的
// 应该是根据快链的Intent来判断是否重复的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
// 但是名称不同时,虽然有的手机系统会显示Toast提示重复,仍然会建立快链
// 屏幕上没有空间时会提示
// 注意:重复创建的行为MIUI和三星手机上不太一样,小米上似乎不能重复创建快捷方式 // 名字
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name); // 图标
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(MainActivity.this,
R.drawable.ic_launcher)); // 设置关联程序
Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
launcherIntent.setClass(MainActivity.this, MainActivity.class);
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); addShortcutIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent); // 发送广播
sendBroadcast(addShortcutIntent);
}

移除快捷方式

  移除快捷方式的action:

public static final String ACTION_REMOVE_SHORTCUT = "com.android.launcher.action.UNINSTALL_SHORTCUT";

  移除快捷方式的方法:

    private void removeShortcut(String name) {
// remove shortcut的方法在小米系统上不管用,在三星上可以移除
Intent intent = new Intent(ACTION_REMOVE_SHORTCUT); // 名字
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name); // 设置关联程序
Intent launcherIntent = new Intent(MainActivity.this,
MainActivity.class).setAction(Intent.ACTION_MAIN); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent); // 发送广播
sendBroadcast(intent);
}

  在两个手机上测试,发现小米手机上添加了快捷方式后不能移除,三星手机可以。

查询快捷方式

  查询快捷方式是否存在的方法是从网上其他资料那里查来的,但是测试查询的时候失败了,两个手机(小米、三星)都查不到。

  先留着代码以后看看是什么原因吧:

    private boolean hasInstallShortcut(String name) {

        boolean hasInstall = false;

        final String AUTHORITY = "com.android.launcher2.settings";
Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/favorites?notify=true"); // 这里总是failed to find provider info
// com.android.launcher2.settings和com.android.launcher.settings都不行
Cursor cursor = this.getContentResolver().query(CONTENT_URI,
new String[] { "title", "iconResource" }, "title=?",
new String[] { name }, null); if (cursor != null && cursor.getCount() > 0) {
hasInstall = true;
} return hasInstall; }

参考资料

  Android之生成桌面快捷方式(一)

  Android之生成桌面快捷方式(二)

  http://blog.csdn.net/ldj299/article/details/6298452

  http://www.xmumu.com/post/2012-04-01/17357119

  http://www.cnblogs.com/CoolPigs/p/3317234.html

Android添加快捷方式(Shortcut)到手机桌面的更多相关文章

  1. Android添加快捷方式

    private void addShortcutToDesktop() { Intent shortcut = new Intent("com.android.launcher.action ...

  2. Android 发送多个不同的快捷方式(shortcut)到桌面并向其启动的Activity传参

    需求: 对于创建快捷方式到桌面,网上能查到不少资料,但一般都是针对应用程序本身的. 前阵子在做项目时,遇到了一个类似于百度贴吧里面的一个需求:对于每个具体的贴吧,都可以将其发送到桌面(HomeScre ...

  3. android 添加桌面快捷方式

    .在桌面创建快捷方式方法: 方法一:通过长按某一个应用程序的图标在桌面上创建启动该应用程序的快捷方式. 这个方法安装完程序都用户都能实现. 方法二:在应用程序中构建一个Intent,然后以Broadc ...

  4. Android添加桌面快捷方式的简单实现

    核心代码如下: Button bn = (Button) findViewById(R.id.bn); // 为按钮的单击事件添加监听器 bn.setOnClickListener(new OnCli ...

  5. android 多个shortCut快捷方式实现以及对58同城快捷方式的实现思路的研究

    这几天,项目中有个新需求,需要按照模块添加不同的快捷方式到桌面上,从而方便用户的使用.特意进行了研究并分析了下58上面桌面快捷方式的实现. 首先多个shortcut的实现: <activity ...

  6. 张高兴的 Xamarin.Forms 开发笔记:Android 快捷方式 Shortcut 应用

    一.Shortcut 简介 Shortcut 是 Android 7.1 (API Level 25) 的新特性,类似于苹果的 3D Touch ,但并不是压力感应,只是一种长按菜单.Shortcut ...

  7. VC/Wince 实现仿Win8 Metro风格界面3——按钮移动交换、删除、添加快捷方式(附效果图)

    上一篇文章写了如何进行页面滑动切换,今天我讲一下如何实现两个按钮拖动交换位置,包括同一个页面按钮交换或者两个页面之间的按钮交换.另外就是如何拖动删除界面上的快捷方式.按钮交换和拖动删除,这两个功能基本 ...

  8. 转Android APP安装后不在桌面显示图标的应用场景举例和实现方法

    转http://www.cnblogs.com/allenzheng/p/4510725.html#3186608 Android APP安装后不在桌面显示图标的应用场景举例和实现方法 最近在为公司做 ...

  9. android 通过wifi 热点实现手机摄像头数据共享(转)

    原文地址:http://blog.csdn.net/sinat_35845281/article/details/52674946 最近想搞一个新奇的玩意儿~~~ 最近一直在在学习通过两个Androi ...

随机推荐

  1. Java生成BASE64编码

    1.所需jar包: BASE64Encoder.jar 2.在导包过程中,可能会出现工程不识别的情况.即:不能找到BASE64Encoder的jar包.此时,可以有两种方法解决这个问题: a.  只需 ...

  2. Java Class文件详解

    作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) Java Class文件中包含以下信息: [+]view code ClassFile { u4 magic;  ...

  3. Android 网络通信API的选择和实现实例

    Android开发网络通信一开始的时候使用的是AsyncTask封装HttpClient,没有使用原生的HttpURLConnection就跳到了Volley,随着OkHttp的流行又开始迁移到OkH ...

  4. [SDK2.2]Windows Azure Virtual Network (1) 概念

    <Windows Azure Platform 系列文章目录> Windows Azure Virtual Network是非常重要的概念,其主要作用有以下两点: 1.将多台Azure V ...

  5. javascript中遇到的字符串对象处理

    在javascript中对参数处理: 1 <script> 2 function getParam() 3 { 4 urlInfo=window.location.href; //获取当前 ...

  6. .net using使用小结

    在.net中using使用方法有3种,分别是using指令.using定义使用范围.using别名. using指令,即引用命名空间. using System; using System.Colle ...

  7. ES6笔记(1) -- 环境配置支持

    系列文章 -- ES6笔记系列 虽然ES6已经发布一年多了,但在各大浏览器之中的支持度还不是很理想,在这查看ES6新特性支持度 Chrome的最新版本浏览器大部分已经支持,在Node.js环境上支持度 ...

  8. RabbitMQ入门教程——路由(Routing)

    绑定( Bindings)   之前的文章中我们已经创建过bindings,代码如下:         channel.QueueBind(queue: queueName, exchange: EX ...

  9. css背景图

  10. 改变 TMemo 的背景颜色 (Firemonkey)

    说明:展示使用程序码改变 Firemonkey TMemo 的背景颜色. 适用:XE6 源码下载:[原創]Memo改背景色_XE6.zip //---------------------------- ...