一、判断是否已有快捷方式

    private 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;
}
private boolean hasShortcut(Context context,String shortCutName)
{
boolean has = false;
final ContentResolver cr = context.getContentResolver();
final String AUTHORITY = getAuthorityFromPermission(context, "com.android.launcher.permission.READ_SETTINGS"); final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); //确认content Provider中是否有快捷键信息
Cursor c = cr.query(CONTENT_URI,
new String[] {"title","iconResource" },
"title=?",
new String[] {shortCutString.trim()},
null);
if(c != null && c.getCount() > 0){
has= true ;
}
return has;
}

二、添加快捷方式

private void addShortCut(Context mContext)
{ boolean has = hasShortcut(mContext, mContext.getString(R.string.str_app_name)); if(has)
{
return;
} Intent shortCutIntent = null;
int shortCutNameId = R.string.app_name;
int shortCutIconId = R.drawable.app_icon;
String pkg = PACKAGE_NAME; boolean installed = isInstalledApp(mContext); if(installed)
{ Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pkg);
List<ResolveInfo> apps = mContext.getPackageManager().queryIntentActivities(resolveIntent, PackageManager.GET_ACTIVITIES); shortCutIntent = new Intent();
if((apps != null) && (apps.size() != 0))
{
shortCutIntent.setComponent(new ComponentName(pkg, apps.get(0).activityInfo.name));
}
}
else
{
shortCutIntent = new Intent(mContext.getApplicationContext(), AppActivity.class);
} Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(shortCutNameId));
shortcut.putExtra("duplicate", false); //不允许重复创建 //快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(mContext.getApplicationContext(), shortCutIconId);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); //程序入口
shortCutIntent.setAction(Intent.ACTION_MAIN);
shortCutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent); mContext.sendBroadcast(shortcut);
}

三、删除快捷方式

   private void deleteShortCut(Context mContext)
{
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
Intent shortCutIntent = new Intent(mContext.getApplicationContext(), AppActivity.class);
shortCutIntent.setAction(Intent.ACTION_MAIN);
shortCutIntent.addCategory(Intent.CATEGORY_LAUNCHER); // 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(R.string.app_name)); // 注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent); mContext.sendBroadcast(shortcut);
}

四、被快捷方式启动的Activity的Intent-filter

            <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Android中的桌面快捷方式的更多相关文章

  1. Android 点击桌面快捷方式和Notifycation跳转到Task栈顶Activity

    我们一般下载的应用在第一次启动应用的时候都会给我创建一个桌面快捷方式,然后我在网上找了些资料整理下了,写了一个快捷方式的工具类,这样我们以后要创建快捷方式的时候直接拷贝这个类,里面提供了一些静态方法, ...

  2. Windows中创建桌面快捷方式

    Windows中创建桌面快捷方式 -------------- -------------- -------------- --------------

  3. 在deepin系统中制作桌面快捷方式

    在使用deepin-wine 安装一些软件的时候,每次启动都需要到.deepinwine目录下运行deepin-wine xx.exe.笔者在安装过HeidiSql之后,一直苦于这种情况.比较好的解决 ...

  4. 如何在Sitecore CMS中管理桌面快捷方式

    当您在Sitecore的桌面模式下工作时,创建快捷方式很有用.快捷方式允许您在选择特定项目的情况下打开内容编辑器,而无需深入了解内容树. Sitecore 8 Sitecore 7 Sitecore ...

  5. Android创建桌面快捷方式

    在桌面上创建特定界面的快捷入口,icon和title根据请求参数命名.在网上收集的一些相关资 料,在使用intent发送广播的时候,一些型号的收集会有问题,如魅族MX,红米,以及华为,使用setCla ...

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

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

  7. Android桌面快捷方式那些事与那些坑

    原文来自http://blog.zanlabs.com/2015/03/14/android-shortcut-summary/ 将近二个多月没写博客了.之前一段时间一直在搞红包助手,就没抽时间写博客 ...

  8. Android 添加、移除和判断 桌面快捷方式图标

    思路: Launcher为了应用程序能够定制自己的快捷图标,就注册了一个 BroadcastReceiver 专门接收其他应用程序发来的快捷图标定制信息.所以只需要根据该 BroadcastRecei ...

  9. Android中为APP创建快捷方式的原理(自己的理解)

    我们首先来看Android中为APP创建快捷方式的原理: 从图上可以看出,Android大致分7步完成快捷方式的创建: 第一步:Android系统的launcher程序会调用它的pickShortcu ...

随机推荐

  1. SQL存储过程,使用事务(try catch),游标

    CREATE proc [dbo].[Sys_Rebate_Equity] AS )--用户ID ,)--总股权数 BEGIN begin try Begin Transaction --开始事务 D ...

  2. linux下mv命令使用方法

    1.作用mv命令来为文件或目录改名或将文件由一个目录移入另一个目录中.该命令等同于DOS系统下的ren和move命令的组合.它的使用权限是所有用户.2.格式mv [options] 源文件或目录 目标 ...

  3. Flex 中画图工具(drawTool)失效

    做项目的时候画图工具突然失效,解决了半天都不行,最后将画图结束的函数map_drawEndHandler写在方法里面的时候,运行却能够画图了,不知道是什么原理,比较头疼,左思右想,都感觉有点怪怪的,虽 ...

  4. 几个opencv 的iOS的编译问题解决

    一个iOS项目需要用到opencv,而且要支持arm64的,以前有个demo的,只支持32位的.到官网下载了最新支持64位库,结果编译无法通过. google了好久也没法解决,后来问了一个同事,找出原 ...

  5. WPF开发经验

    UpdateSourceTrigger 0.在一个项目中在用到绑定的时候一直有一个问题,虽然设置了Mode=TwoWay,界面的值修改了,但是后天绑定的值没有变化.最终发现了问题,在于UpdateSo ...

  6. Cdnbes负载均衡的权重用法解释

    (1)相同域名添加两条记录,解析不同的ip,可以设置权重,比如权重2,就意思占百分之20 ,数字越大,优先级越大 (2)这个hash 如果用户访问的源是挂掉的.会去第二个源

  7. [原]通过配合ffmpeg.exe获取视频文件时长

    import subprocess import os import time def getTime(flvpath,fid): #file_str = '1.flv' file_str = flv ...

  8. 介绍开源的.net通信框架NetworkComms框架 源码分析(二十二 )TCPConnectionStatic

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...

  9. (转载)HTML5 LocalStorage 本地存储

    原文地址:http://www.cnblogs.com/xiaowei0705/archive/2011/04/19/2021372.html HTML5 LocalStorage 本地存储 说到本地 ...

  10. 忘记BIOS超级管理员密码,怎么破解?

    [请尊重原创版权,如需引用,请注明来源及地址] 本人就喜欢没事瞎折腾,动动手活动活动筋骨没坏处,前不久非常便宜的弄到一玩具 ThinkPad T400(公司处理品),外观还算不错,除了电源适配器是坏的 ...