前言

在某些情况下需要对应用创建桌面快捷方式,特别是在使用原生系统(4.3到8.0)运行物联网app时,往往存在二级菜单,而应用图标默认安装在二级菜单(现在国内主流手机厂商的系统都没有二级菜单了),如果按照网上的做法是可以创建和兼容8.0系统,但存在一个问题:

点击快捷方式打开应用和点击二级菜单应用图标打开应用,会出现重启应用的问题,导致两边进入应用显示的页面内容不一致,体验极不友好,比如:通过快捷方式进入应用并经过一系列操作进入到看某网红直播的页面,然后按home键,进入系统二级菜单点击了应用图标,这时发现应用重启,真操蛋。

解决方法

为 快捷方式意图设置如下代码即可:

shortcutInfoIntent.setClassName(activity, activity.getClass().getName());
        shortcutInfoIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        shortcutInfoIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
        shortcutInfoIntent.addCategory(Intent.CATEGORY_LAUNCHER);

这样不管那边启动应用,两边界面内容一致

操作步骤

1.添加权限 

 <!--快捷方式--><uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /><!-- 添加快捷方式 --><uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /><!-- 移除快捷方式 --><uses-permission android:name="android.permission.INSTALL_SHORTCUT" /><uses-permission android:name="android.permission.UNINSTALL_SHORTCUT" /><!-- 查询快捷方式2.1以下 --><uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /><uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" /><!-- 查询快捷方式4.4及以下 --><uses-permission android:name="com.android.launcher2.permission.READ_SETTINGS" /><uses-permission android:name="com.android.launcher2.permission.WRITE_SETTINGS" /><!-- 查询快捷方式4.4以上 --><uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS" /><uses-permission android:name="com.android.launcher3.permission.WRITE_SETTINGS" />

2.使用工具类在启动页操作

该工具类已经适配4.3到8.0快捷方式创建

package com.sjl.core.util;import android.app.Activity;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;/*** 快捷方式创建工具类** @author Kelly* @version 1.0.0* @filename ShortcutUtils.java* @time 2019/5/28 17:35* @copyright(C) 2019 song*/
public class ShortcutUtils {private static final String TAG = "ShortcutUtils";/*** 添加桌面图标快捷方式** @param activity Activity对象,设置要启动的activity,一般都是应用入口类* @param nameId   快捷方式名称id* @param iconId   图标资源id*/public static void addShortcut(Activity activity, int nameId, int iconId) {Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), iconId, null);addShortcut(activity, activity.getResources().getString(nameId), bitmap);}/*** 添加桌面图标快捷方式** @param activity Activity对象* @param name     快捷方式名称* @param icon     快捷方式图标*/public static void addShortcut(Activity activity, String name, Bitmap icon) {Intent shortcutInfoIntent = new Intent(Intent.ACTION_MAIN);/*** 点击快捷方式回到应用,而不是重新启动应用,解决系统一级菜单和二级菜单进入应用不一致问题*/shortcutInfoIntent.setClassName(activity, activity.getClass().getName());shortcutInfoIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);shortcutInfoIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);shortcutInfoIntent.addCategory(Intent.CATEGORY_LAUNCHER);if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {if (isShortCutExist(activity, name)) {Log.w(TAG, "shortcut already exist.");return;}//  创建快捷方式的intent广播Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");// 添加快捷名称shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);//  快捷图标是允许重复(不一定有效)shortcut.putExtra("duplicate", false);// 快捷图标// 使用资源id方式
//            Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(activity, R.mipmap.icon);
//            shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);// 使用Bitmap对象模式shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);// 添加携带的下次启动要用的Intent信息shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutInfoIntent);// 发送广播activity.sendBroadcast(shortcut);} else {ShortcutManager shortcutManager = (ShortcutManager) activity.getSystemService(Context.SHORTCUT_SERVICE);if (null == shortcutManager) {// 创建快捷方式失败Log.e(TAG, "Create shortcut failed.ShortcutManager is null.");return;}shortcutInfoIntent.setAction(Intent.ACTION_VIEW); //action必须设置,不然报错ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(activity, name).setShortLabel(name).setIcon(Icon.createWithBitmap(icon)).setIntent(shortcutInfoIntent).setLongLabel(name).build();shortcutManager.requestPinShortcut(shortcutInfo, PendingIntent.getActivity(activity,0, shortcutInfoIntent, PendingIntent.FLAG_UPDATE_CURRENT).getIntentSender());}}/*** 判断快捷方式是否存在** @param context 上下文* @param title   快捷方式标志,不能和其它应用相同* @return*/public static boolean isShortCutExist(Context context, String title) {boolean isInstallShortcut = false;if (null == context || TextUtils.isEmpty(title))return isInstallShortcut;String authority = getAuthority();final ContentResolver cr = context.getContentResolver();if (!TextUtils.isEmpty(authority)) {try {final Uri CONTENT_URI = Uri.parse(authority);//  Cursor c = cr.query(CONTENT_URI, new String[]{"title", "iconResource"}, "title=?", new String[]{title.trim()},null);Cursor c = cr.query(CONTENT_URI, new String[]{"title"}, "title=?", new String[]{title.trim()},null);// XXX表示应用名称。if (c != null && c.getCount() > 0) {isInstallShortcut = true;}if (null != c && !c.isClosed())c.close();} catch (Exception e) {Log.e(TAG, "isShortCutExist:" + e.getMessage());}}return isInstallShortcut;}public static String getAuthority() {String authority;int sdkInt = android.os.Build.VERSION.SDK_INT;if (sdkInt < 8) { // Android 2.1.x(API 7)以及以下的authority = "com.android.launcher.settings";} else if (sdkInt <= 19) {// Android 4.4及以下authority = "com.android.launcher2.settings";} else {// 4.4以上authority = "com.android.launcher3.settings";}return "content://" + authority + "/favorites?notify=true";}}

3.代码调用

        ShortcutUtils.addShortcut(SplashActivity.this, R.string.app_name, R.mipmap.ic_launcher);

Android小技巧之创建桌面快捷方式(已适配8.0)相关推荐

  1. android自动创建桌面,Android应用启动后自动创建桌面快捷方式的实现方法

    Android的开发其实是比较灵活的,其实在安装了Android应用程序之后,会在桌面上自动创建快捷方式,接下来爱站技术频道小编将会介绍Android应用启动后自动创建桌面快捷方式的实现方法给大家,有 ...

  2. [Android]为指定的应用创建桌面快捷方式

    网上一搜一大把为自己的应用创建快捷方式,但是本文的侧重点在为"指定的应用"创建桌面快捷方式.     常见的桌面快捷方式有两要素:1.应用名 2.应用图标. 指定应用图标的信息是: ...

  3. Android应用启动后自动创建桌面快捷方式

    为什么80%的码农都做不了架构师?>>>    和IOS开发和Windows Phone开发相比,Android是开放的,Android上的开发也相对更加灵活,能够做很多事情.有的朋 ...

  4. Android应用程序创建桌面快捷方式

    2019独角兽企业重金招聘Python工程师标准>>> public static final String READ_SETTINGS_PERMISSION = "com ...

  5. android手机自动快捷方式,解析Android应用启动后自动创建桌面快捷方式的实现方法...

    要不怎么说Android特别开放呢,在Android开发中,只要发送一个广播,就可以实现这种需求了. 废话不多说,以下是封装好的一段代码. public class ShortcutUtil { pu ...

  6. Android 8.0 创建桌面快捷方式

    1. 前言:公司有个给app创建桌面快捷方式的功能.  有一天,测试来说,有款手机,点击快捷方式,无反应.  2.思考:大多数手机是好的,考虑是兼容性问题. 1)经查看,问题机是  华为mate9  ...

  7. 如何在Ubuntu上创建桌面快捷方式

    Desktop icons should be simple, but they're not on Ubuntu 18.04 LTS and newer releases like Ubuntu 1 ...

  8. edge浏览器如何把网页放到桌面_win10系统怎么把Edge浏览器放到桌面?Edge怎么创建桌面快捷方式...

    在安装win10系统之后,很多用户都会发现win10系统采用全新的Microsoft Edge浏览器,使用之后觉得Edge浏览器功能强大,不过在桌面并没有Edge浏览器快捷方式图标,那么要怎么在桌面创 ...

  9. win10计算机到桌面显示器,win10系统创建桌面快捷方式关闭电脑显示器的操作方法...

    win10系统创建桌面快捷方式关闭电脑显示器的操作方法? 很多win10用户在使用电脑的时候,会发现win10系统创建桌面快捷方式关闭电脑显示器的的现象,根据小编的调查并不是所有的朋友都知道win10 ...

最新文章

  1. 网游生命周期在百度指数曲线上呈“M”形分布,各阶段搜索行为呈一定特征
  2. intel服务器主板芯片,英特尔® 服务器主板 S2600CW2SR
  3. 原创jquery插件treeTable(转)
  4. Bootatrap中的表单(2)
  5. nodejs TCP服务器和客户端通信的socket结构
  6. 单片机小白学步系列(〇)序
  7. CentOS 下 Oracle 10g 安装 + 配置 全过程(图解)
  8. NSString的retainCount
  9. 我有你没有游戏例子100_50米的决赛圈里面藏着100个人?光子:知道什么叫质量局了吧!...
  10. 面试系列(三):Java反射机制
  11. 【实用软件】picasa不能导入文件夹至其中 的问题解决
  12. 从空运物流到无人机物流,圆通将战火烧到了时效件
  13. 免费大数据平台有哪些?
  14. 学计算机要数学吗,学习计算机真的需要数学能力超强吗?
  15. 简约至上:你必须知道的产品设计
  16. 《Fundamentals of Computer Grahpics》虎书第三版翻译——第一章 介绍
  17. 基于VS+Opencv2.4.10的微信跳一跳辅助工具
  18. 校园助手APP--简介及框架
  19. 【FAQ】接入HMS Core推送服务过程中一些常见问题总结
  20. SQL注入攻击的原理、分类和防御方法

热门文章

  1. Seata流程源码梳理上篇-TM、RM处理
  2. 计算机技术应用基础书,计算机应用基础教程
  3. Web自动化神器,批量下载小姐姐美图,可直接导入使用
  4. python matplotlib 绘制不等距数据
  5. java P1506 拯救oibh总部
  6. oracle核销预付账款,遇到预付款发票完全核销,而没有核销记录!
  7. PDU——协议数据单元
  8. 【AUTOSAR-IpduM】-3.1-配置一个发送Tx Dynamic Container PDU(Multiple-PDU)
  9. 关于background-size各个参数详解
  10. 如何让右键菜单出现“命令行在这里”,即cmd here