Android(安卓)为某个App 添加/移除 启动快捷方式、书签快捷方式、应用市场快捷方式
16lz
2021-01-26
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
更多相关文章
- Android中ProgressDialog的使用
- Kotlin中的Android项目编译出现 Unresolved reference: R 或者co
- TextView和EditText中添加图片(ImageSpan)
- 常用的Android(安卓)Widget组件学习③-checkbox和radiogroup的介
- android 拦截器
- BLE 扫描及连接 android程序开发(一)
- android待机添加电量小部件,点击位置信息图标,模式切换需要改为四
- Android设置Toast长时间显示,及AlertDialog的使用
- Android(安卓)studio--Android开发添加外来字体库