Android创建应用快捷方式(ShortCut)的有效方式
2016-04-18 09:24
766 查看
有时候我们需要为自己的APP,创建桌面快捷方式,本文将介绍有效的方式,避免出现重复创建,或者创建失败的情况。通常应用安装后,第一次打开,去检测当前应用的快捷方式是否创建,如果没有创建,我们就帮生成桌面shortcut,否则不创建。
PS:最好不要每次进入应用都去创建,这样可能频繁提示用户快捷图标已经创建。
下面贴代码介绍生成应用快捷方式的工具类。
PS:最好不要每次进入应用都去创建,这样可能频繁提示用户快捷图标已经创建。
下面贴代码介绍生成应用快捷方式的工具类。
1.创建快捷方式:发送广播
需要设置好程序名,图标,和启动的Activity。public static void createShortCut(Context contxt) { //String applicationName=getApplicationName(contxt); String applicationName = UIUtils.getString(R.string.app_name);//程序名称,不是packageName if (isInstallShortcut(contxt,applicationName)) {// 如果已经创建了一次就不会再创建了 return; } Intent sIntent = new Intent(Intent.ACTION_MAIN); sIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 加入action,和category之后,程序卸载的时候才会主动将该快捷方式也卸载 sIntent.setClass(contxt, SplashActivity.class);//点击后进入的Activity Intent installer = new Intent(); installer.putExtra("duplicate", false);//false标示不重复创建 installer.putExtra("android.intent.extra.shortcut.INTENT", sIntent); //设置应用的名称 installer.putExtra("android.intent.extra.shortcut.NAME", applicationName); //设置图标 installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(contxt, R.drawable.ic_launcher)); installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); contxt.sendBroadcast(installer);//发送安装桌面图标的通知 }
2.检测是否创建过该快捷方式
使用ContentResolver查询数据库,看是否存在同名的shortcut。因为历史原因,用两个launcher,需要根据sdk版本进行查询。public static boolean isInstallShortcut(Context context,String applicationName) { boolean isInstallShortcut = false; ContentResolver cr = context.getContentResolver(); //sdk大于8的时候,launcher2的设置查找 String AUTHORITY = "com.android.launcher2.settings"; Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); Cursor c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { applicationName }, null); if (c != null && c.getCount() > 0) { isInstallShortcut = true; } if (c != null) { c.close(); } //如果存在先关闭cursor,再返回结果 if (isInstallShortcut) { return isInstallShortcut; } //android.os.Build.VERSION.SDK_INT < 8时 AUTHORITY = "com.android.launcher.settings"; CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] {applicationName}, null); if (c != null && c.getCount() > 0) { isInstallShortcut = true; } if (c != null) { c.close(); } return isInstallShortcut; }
3.获取应用程序的名称:ApplicationName
如果上面的ApplicationName不是通过xml中的 String获取,也可以程序获取。//获取当前app的应用程序名称 public static String getApplicationName(Context context) { PackageManager packageManager = null; ApplicationInfo applicationInfo = null; try { packageManager = context.getApplicationContext().getPackageManager(); applicationInfo=packageManager.getApplicationInfo(context.getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { applicationInfo = null; } String applicationName =(String) packageManager.getApplicationLabel(applicationInfo); return applicationName; }
如何删除shortcut呢?
如果用户卸载我们的APP,通常快捷方式是会自动删除的,不需要自己处理。当然,我们程序也可以检测到已经有了快捷方式,删除它重新创建,其实原理如下://删除shortcut public static void delShortcut(Context cx) { Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); // 获取当前应用名称 String title = null; try { final PackageManager pm=cx.getPackageManager(); title=pm.getApplicationLabel(pm.getApplicationInfo(cx.getPackageName(),PackageManager.GET_META_DATA)).toString(); } catch (Exception e) { } // 快捷方式名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); Intent shortcutIntent = cx.getPackageManager().getLaunchIntentForPackage(cx.getPackageName()); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent); cx.sendBroadcast(shortcut); }
未完待续,欢迎交流。杜工,Dusan,Q:291902259.
相关文章推荐
- Android Studido下的应用性能优化总结--布局优化
- android开发之&使用viewFlipper实现轮播图,超级简单
- Android framework回想(2) sp 和 wp sp对象
- Android程序开发之手机APP创建桌面快捷方式
- kernel logo到开机动画之间闪现黑屏(android 5.X)
- Android Apk打包过程概述:Android是如何打包apk的
- Android 热修补方案(AndFix)
- Android之智能问答机器人
- android pdf 阅读器开发, pdf demo, pdf第三方控件
- Mac Android Genymotion unable create virtual device
- Android studion下载GsonFormat插件
- Android开发周刊 第二期
- Android发送短信界面
- 第七周Android学习笔记
- Android——黑名单管理(二)
- Android Studio 快捷键
- 转一篇eoe论坛上sfshine大神的文章--AndroidHal底层! ConsumerIrService分析
- android中常用的弹出提示框
- 【Android】如何快速构建Android Demo
- 【Android】如何实现ButterKnife