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

    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. 关于 矩阵在ACM中的应用

    关于矩阵在ACM中的应用 1.矩阵运算法则 重点说说矩阵与矩阵的乘法,不说加减法. 支持: 结合律  (AB)C = A(BC) 分配律 A(B+C) = AB + AB $\left( \lambd ...

  2. nagios二次开发(六)---nagiosql原理及主要文件的介绍

    nagiosql的入口文件:index.php,这也是所有php程序的入口文件.是由apache指定的. index.php 文件的开始引入了 require("functions/prep ...

  3. LoadRunner --HTML/URL录制方式的选择规则

  4. android 设置状态栏与标题背景颜色一致

    必须在Android4.4以上版本才能设置状态栏颜色: 一.在单个Activity里面,设置状态栏的背景: 效果: 1.在Activity的布局根文件中添加属性: android:fitsSystem ...

  5. OpenGL ES crash notes 01 - Nice to meet you

    这篇笔记完全参照<OpenGL.ES.3.0.Programming.Guide.2nd.Edition>,摘出部分内容只为学习参考. 为什么要用英文:无论是D3D的SDK还是OES的Sp ...

  6. CC1310电源管脚

    对于48pin脚的CC1310而言,属于电源类的管脚如下: 上述电源类管脚的关系如下: 1 VDDS类管脚 VDDS类管脚包括VDDS.VDDS2.VDDS3和VDDS_DCDC四个管脚.其中VDDS ...

  7. 在table中进行内容搜索

    $("tbody td").filter(":contains('" + x + "')").css('color','red').pare ...

  8. IT行业果真跳槽快吗?

    近年来IT行业越来越火爆,许多人也开始炒,月入万元不是梦,随随便便拿高薪之类的文章层出不穷,许多的青少年甚至中年人开始关注这块,许多人选择去学习it行业,也朝着月入万元的目标前进,然而,曾几何时,月入 ...

  9. SonarQube-5.6.3 代码分析平台搭建使用

    python代码分析 官网主页: http://docs.sonarqube.org/display/PLUG/Python+Plugin Windows下安装使用: 快速使用: 1.下载jdk ht ...

  10. PHP实现CSV大文件数据导入到MYSQL数据库

    <?php $db_host="192.168.1.10"; $db_user="root"; $db_psw="11111"; $d ...