有时候我们需要为自己的APP,创建桌面快捷方式,本文将介绍有效的方式,避免出现重复创建,或者创建失败的情况。通常应用安装后,第一次打开,去检测当前应用的快捷方式是否创建,如果没有创建,我们就帮生成桌面shortcut,否则不创建。
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,通常快捷方式是会自动删除的,不需要自己处理。当然,我们程序也可以检测到已经有了快捷方式,删除它重新创建,其实原理如下:

//删除shortcutpublic 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.

更多相关文章

  1. android在TextView的最后一行后面拼接view(类似长文本的查看更多
  2. 【自定义控件系列一】android画图类---Canvas浅谈
  3. 【Android】获取当前 Wifi 的 MAC 地址(BSSID),注意并非是手机的MAC
  4. 不同手机在豌豆夹上显示设备同名的解决方法 && Android(安卓)获
  5. 关于android各种双卡手机获取imei,imsi的处理(mtk,展讯,高通等)
  6. android学习之获取联系人原理(姓名和电话)详解
  7. Android中获取后台正在运行的应用列表(附源码)
  8. android 验证码按钮点击,判断网络和匹配手机号并自动更新时间;
  9. 记录Android中两种坐标系和获取View坐标值、相对距离的一点相关

随机推荐

  1. php学习小结(类成员重载、全局成员、空间
  2. 定位的类型与应用场景和使用条件
  3. 身为在软件测试摸爬滚打多年工程师的感悟
  4. 我的第31个代码
  5. grid属性布局
  6. flex实战京东页面
  7. 2021-02-26:一个数组arr是二叉树的中序遍
  8. C# 数据操作系列 - 9. EF Core 完结篇
  9. C# 数据操作系列 - 10 NHibernate初试
  10. C# 数据操作系列 - 12 NHibernate的增删