/**
* 创建添加快捷方式
* 其中需要设置的有:
* 1. 快捷方式的标题
* 2. 快捷方式的图标
* 3. 点击快捷方式后的跳转
*/
public static void createShortcut(Context context) {
if(haveShortCut(context,context.getResources().getString(R.string.app_bind_phone_num))){
APP.showToast("已经创建了快捷方式");
Toast.makeText(context,"已经创建了快捷方式",Toast.LENGTH_LONG).show();
return;
}
// 创建添加快捷方式的Intent
Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 取得快捷方式的标题
String title = context.getResources().getString(R.string.app_bind_phone_num);
// 加载快捷方式的图标
Parcelable icon = Intent.ShortcutIconResource.fromContext(context, R.drawable.online_bookshelf); // 创建点击快捷方式后操作Intent,该处当点击创建的快捷方式后,再次启动该程序
// Intent myIntent = new Intent(context, com.zhangyue.iReader.main.MainActivity.class);
Intent myIntent=new Intent();
// 设置快捷方式的标题
// myIntent.setClassName(context,"com.ireader.plug.sdk.MainActivity");
myIntent.setClassName(context,"com.zhangyue.iReader.main.MainActivity");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
// 设置快捷方式的图标
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 设置快捷方式对应的Intent
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);
// 发送广播添加快捷方式
context.sendBroadcast(addIntent);
} public static boolean haveShortCut(Context context,String title){
boolean isInstallShortcut = false;
final ContentResolver cr = context.getContentResolver();
// 本人的2.2系统是”com.android.launcher2.settings”,网上见其他的为"com.android.launcher.settings"
// final String AUTHORITY = "com.android.launcher2.settings";
final String AUTHORITY = getAuthorityFromPermission(context,"com.android.launcher.permission.READ_SETTINGS");
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/favorites?notify=true");
Cursor c = cr.query(CONTENT_URI,
new String[] { "title", "iconResource" }, "title=?",
new String[] { title }, null);// XXX表示应用名称。
if (c != null && c.getCount() > 0) {
isInstallShortcut = true;
System.out.println("已创建");
}
return isInstallShortcut;
}
public static String getAuthorityFromPermission(Context context, String permission){
if (permission == null) return null;
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
if (packs != null) {
for (PackageInfo pack : packs) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
if (permission.equals(provider.readPermission)) return provider.authority;
if (permission.equals(provider.writePermission)) return provider.authority;
}
}
}
}
return null;
}

我写的这个一定可以运行。创建快捷方式之前先查询有没有,有了Toast告诉用户,没有了,去创建。 查询快捷方式有没有的时候, 关于Launcher的快捷方式信息是 存放在 com.android.launcher的launcher.db的favorites表 中的,所以,创建,查询,删除动作都需要操作到那张表。

可能广大的网友 还会发现还会有部分手机判断是否存在快捷方式的时候信息还是不准确。会出现”Failed to find provider info for com.android.calendar”的error,既不是launcher也不是launcher2,那到底哪里出现了问题了呢?

所以,我们需要去查询provider获取真实的content的uri前缀,这样才能保证查询的时候匹配成功。提供获取真实字串的函数如下:

getAuthorityFromPermission这个方法就是干这个的。

如果你是插件程序,你想要创建快捷方式,那么你的activity一定要是expolde=”true”,不然会提示你手机上没有安装该应用。

Android 快捷方式的创建与查询 快捷方式问题大全 获取快捷方式在Launcher数据库中的信息 Failed to find provider info for com.android.la的更多相关文章

  1. Android+PHP+MYSQL把数据库中的数据显示在Android界面上

    俗话说,好记性不如烂笔头.今天终于体会其中的道理了.昨天写好的代码不知道为何找不到了.所以今天我一定得抽出一点时间把我的代码保存起来,以防我的代码再没有了. 还是先上图片. 这个界面是用ListVie ...

  2. Android之运行时相机权限和联系人权限获取

    原文:Android之运行时相机权限和联系人权限获取 本文链接:http://blog.csdn.net/qq_16628781/article/details/61623502 Android之运行 ...

  3. Android开发之创建桌面快捷方式

    Android创建桌面快捷方式就是在桌面这个应用上创建一个快捷方式,桌面应用:launcher2 通过查看launcher2的清单文件: <!-- Intent received used to ...

  4. 【转】Android 快捷方式的创建

    http://blog.csdn.net/lenmoyouzi/article/details/16939977 一.在日常开发中,我们经常会遇到这样的需求就是网桌面添加快捷方式:常见的快捷方式有两种 ...

  5. Python创建、删除桌面、启动组快捷方式的例子分享

    一.Python创桌面建快捷方式的2个例子 例子一: 代码如下: import osimport pythoncomfrom win32com.shell import shell    from w ...

  6. SQL Server 查询分析器提供的所有快捷方式(快捷键)

    SQL Server程序员经常要在SSMS(SQL Server Management Studio)或查询分析器(2000以前)中编写T-SQL代码.以下几个技巧,可以提升工作效率. 以下说明以SS ...

  7. Android开发 ---代码创建选项菜单、隐藏菜单项、菜单的生命周期,菜单按钮图标设置、搜索框、xml中设置子菜单

    1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> < ...

  8. android 之fragment创建

    1.使用xml标签 1.1定义两个重要属性  <fragment         android:id="@+id/fregment_top"        android: ...

  9. Linq专题之创建Linq查询表达式

    本节我们主要介绍一下如何创建查询集合类型,关系数据库类型,DataSet对象类型和XML类型的数据源的Linq查询表达式. 下面在实例代码ReadyCollectionData()函数创建了准备的数据 ...

随机推荐

  1. 使用python做最简单的爬虫

    使用python做最简单的爬虫 --之心 #第一种方法import urllib2 #将urllib2库引用进来response=urllib2.urlopen("http://www.ba ...

  2. PHP:substr和mb_substr的区别

    substr和mb_substr函数都是获取字符串中的某个部分 那么,它们的区别在哪儿呢? 区别: substr :全部是英语.数字就正常:但有一些的字元是占用多个位元的,substr()就得不到你预 ...

  3. Cholesky分解(Cholesky decomposition / Cholesky )

    Cholesky decomposition In linear algebra, the Cholesky decomposition or Cholesky is a decomposition ...

  4. April 16 2017 Week 16 Sunday

    Happiness is a way station between too much and too little. 幸福就是刚刚好. I don't want to talk about it a ...

  5. April 7 2017 Week 14 Friday

    A good heart is better than all the brains in the world. 聪明绝顶,不如宅心仁厚. A good heart can be useful to ...

  6. UI5 Source code map机制的细节介绍

    在我的博客A debugging issue caused by source code mapping里我介绍了在我做SAP C4C开发时遇到的一个曾经困扰我很久的问题,最后结论是这个问题由于Jav ...

  7. [译文]PHP千年虫(y2k compliance)

    时钟将我们无情地逼近2000年的最后一年,第二年厄运塞耶斯都预言前所未有的电脑故障在每一个可以想象的领域.通常被称为2000年问题,或千年虫,这种 情况很容易解释.程序解释两位在形成XX日期19 XX ...

  8. 2019.03.13 ZJOI2019模拟赛 解题报告

    得分: \(55+12+10=77\)(\(T1\)误认为有可二分性,\(T2\)不小心把\(n\)开了\(char\),\(T3\)直接\(puts("0")\)水\(10\)分 ...

  9. LA 3938 动态最大连续和

    题目链接:https://vjudge.net/contest/146667#problem/C 题意:动态的求一个区间的最大连续和. 分析: 看上去可以RMQ去做,但是,当分成两个部分,原来的部分的 ...

  10. Kruskal算法求最小生成树(POJ2485)

    题目链接:http://poj.org/problem?id=2485 #include <iostream> #include <stdio.h> #include < ...