博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用Android 8.0 ShortcutManager创建桌面快捷图标
阅读量:2230 次
发布时间:2019-05-09

本文共 2033 字,大约阅读时间需要 6 分钟。

 

创建方法:

/**     @param context 当前content     @param targetClass 快捷图标打开的界面     @param backClass 打开后按返回键返回的界面     @param shortCutId shortCut 唯一id     @param shortCutIcon 桌面上显示的图标     @param shortCutLabel 桌面图标下方显示的文字     */    public static void AddShortCut(Context context, Class targetClass, Class backClass, String shortCutId, int shortCutIcon, String shortCutLabel) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);            if (shortcutManager != null && shortcutManager.isRequestPinShortcutSupported()) {                Intent shortcutInfoIntent = new Intent(context, targetClass);                shortcutInfoIntent.setAction(Intent.ACTION_VIEW);                 ShortcutInfo info = new ShortcutInfo.Builder(context, shortCutId).setIcon(Icon.createWithResource(context, shortCutIcon))                                                                                 .setShortLabel(shortCutLabel)                                                                                 .setIntent(shortcutInfoIntent)                                                                                 .build();                                PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, backClass), PendingIntent.FLAG_UPDATE_CURRENT);                                shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());            }        }        else        {            ToastUtils.show("设备不支持在桌面创建快捷图标!");        }            }

调用:

//长按创建桌面快捷图标dicBtn.setOnLongClickListener(v -> {               Utils.AddShortCut(getContext(),                                       DictionaryActivity.class,                       MainActivity.class,                       "shorcut_dictionary",                       R.drawable.shorcut_dictionary,                       getResources().getString(R.string.shorcut_dictionary)    );    return true;});

 

转载地址:http://blkbb.baihongyu.com/

你可能感兴趣的文章
hibernate 时间段查询
查看>>
java操作cookie 实现两周内自动登录
查看>>
Tomcat 7优化前及优化后的性能对比
查看>>
Java Guava中的函数式编程讲解
查看>>
Eclipse Memory Analyzer 使用技巧
查看>>
tomcat连接超时
查看>>
谈谈编程思想
查看>>
iOS MapKit导航及地理转码辅助类
查看>>
检测iOS的网络可用性并打开网络设置
查看>>
简单封装FMDB操作sqlite的模板
查看>>
iOS开发中Instruments的用法
查看>>
强引用 软引用 弱引用 虚引用
查看>>
数据类型 java转换
查看>>
"NetworkError: 400 Bad Request - http://172.16.47.117:8088/rhip/**/####t/approval?date=976
查看>>
mybatis 根据 数据库表 自动生成 实体
查看>>
C结构体、C++结构体、C++类的区别
查看>>
进程和线程的概念、区别和联系
查看>>
CMake 入门实战
查看>>
绑定CPU逻辑核心的利器——taskset
查看>>
Linux下perf性能测试火焰图只显示函数地址不显示函数名的问题
查看>>