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

    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. RequireJS基础(二)

    上一篇是把整个jQuery库作为一个模块.这篇来写一个自己的模块:选择器. 为演示方便这里仅实现常用的三种选择器id,className,attribute. RequireJS使用define来定义 ...

  2. WEB测试方法及注意地方

    1页面部分(1) 页面清单是否完整(是否已经将所需要的页面全部都列出来了)(2) 页面是否显示(在不同分辨率下页面是否存在,在不同浏览器版本中页面是是否显示)(3) 页面在窗口中的显示是否正确.美观( ...

  3. Unity 几种优化建议

    转: http://user.qzone.qq.com/289422269/blog/1453815561?ptlang=2052 Unity 几种优化建议 最简单的优化建议: 1.PC平台的话保持场 ...

  4. Modelica学习

    Annotation Choices for Suggested Redeclarations and Modifications Replaceable model sample(start,int ...

  5. PadLeft 和 PadRight

    1 PadLeft 即:向已知字符串左边补充字符,使整个字符串到达指定长度 CREATE FUNCTION PadLeft ( ),/*原始字符*/ @TotalLength int,/*总长度*/ ...

  6. 发现IE7的一个问题,不能用索引取字符串中的单个字符

    如下javascript: var testValue="hello,world"; alert(testValue[]); 在IE7上运行该代码,竟然提示值为"unde ...

  7. 学习C:打印输入中单词长度的水平方向直方图

    #include <stdio.h>#define IN 1#define OUT 0#define MAXWL 16 main() { /*打印输入单词长度的水平直方图*/ int c, ...

  8. ios 中 documents和library 的区别

    简单来说就是用户在APP中输入并保存的数据放在Documents文件夹中(如用户输入的文本等), 并且如果手机连接电脑时,iTunes会自动备份其中文件,苹果不允许我们将下载的大型文件放入该文件夹. ...

  9. reference

    z@z:~$ g++ -std=c++11 ref.cpp ref.cpp:7:10: error: invalid conversion from 'int' to 'int*' [-fpermis ...

  10. 让padding、border等不占据宽度

    box-sizing:border-box; -moz-box-sizing:border-box; /* Firefox */ -webkit-box-sizing:border-box; /* S ...