1. 在AndroidManifest文件中添加uses permission

<!-- 添加快捷方式 --><uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /><!-- 移除快捷方式 --><uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
2. 为其他应用添加应用程序的快捷方式(以微信为例)
String packageName = "com.tencent.mm";//以微信为例PackageManager pManager = context.getPackageManager();Intent launchIntent = pManager.getLaunchIntentForPackage(packageName);List<ResolveInfo> infoList = pManager.queryIntentActivities(launchIntent, PackageManager.GET_ACTIVITIES);ResolveInfo launchInfo = infoList.get(0);String name = launchInfo.loadLabel(pManager).toString();String launchActName = launchInfo.activityInfo.name;int iconId = launchInfo.activityInfo.applicationInfo.icon;Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT);// 添加名称addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);// 添加图标Context pkgContext;try {pkgContext = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(pkgContext, iconId);addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);// 添加ComponentComponentName compName = new ComponentName(packageName, launchActName);Intent extraIntent = new Intent(Intent.ACTION_MAIN);extraIntent.setComponent(compName);addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, extraIntent);// 不允许重复添加addShortcutIntent.putExtra("duplicate", false);context.sendBroadcast(addShortcutIntent);

3. 添加书签快捷方式/应用市场快捷方式(以微信为例)

// 以微信为例Parcelable icon = Intent.ShortcutIconResource.fromContext(context, R.drawable.wechat_logo);String name = "wechat";String package = "com.tencent.mm";Intent intentAddShortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");// 添加名称intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);// 添加图标intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);Intent launchIntent = new Intent(Intent.ACTION_VIEW);// 添加应用市场链接String appAddr = "market://details?id=" + packageName;// 添加网页链接// String appAddr = "http://www.baidu.com";launchIntent.setData(Uri.parse(appAddr));intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);intentAddShortcut.putExtra("duplicate", false);context.sendBroadcast(intentAddShortcut

4. 移除快捷方式:

移除快捷方式与添加快捷方式几乎完全相同,唯一的不同是移除的Intent的Action是"com.android.launcher.action.UNINSTALL_SHORTCUT";

5. 判断手机上是否安装了某个App

public boolean hasApp(String packageName) {PackageManager pManager = context.getPackageManager();try {pManager.getApplicationInfo(packageName, 0);} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();return false;}return true;}

备注:

问:如何获得某个应用的package name

答:进入豌豆荚市场,搜索某个应用,进入其详情页面。在地址栏中可以查看到应用的包名:



附上我封装好的类:http://download.csdn.net/download/u011268102/5759383

更多相关文章

  1. Android中ProgressDialog的使用
  2. Kotlin中的Android项目编译出现 Unresolved reference: R 或者co
  3. TextView和EditText中添加图片(ImageSpan)
  4. 常用的Android(安卓)Widget组件学习③-checkbox和radiogroup的介
  5. android 拦截器
  6. BLE 扫描及连接 android程序开发(一)
  7. android待机添加电量小部件,点击位置信息图标,模式切换需要改为四
  8. Android设置Toast长时间显示,及AlertDialog的使用
  9. Android(安卓)studio--Android开发添加外来字体库

随机推荐

  1. 原生购物车 Vue 改写
  2. docker入门到进阶三
  3. docker入门到进阶二
  4. OpenStack部署
  5. 使用PHPExcel导出excell,出现身份证用科学
  6. Xml简介_动力节点Java学院整理
  7. xpath简介_动力节点Java学院整理
  8. 简单了解XML 树结构
  9. Xpath语法格式总结
  10. SyntaxHighlighter 去掉右侧滚动条的方法