Android O添加桌面快捷方式的示例
作者:Maxiye 时间:2022-12-27 07:53:19
手机升级到安卓O后,突然发现创建快捷方式的功能失效了,查询一番后发现:安卓O要使用ShortcutManager来创建快捷方式。
安卓N及以下版本:
Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//"com.android.launcher.action.INSTALL_SHORTCUT"
// 不允许重复创建
addShortcutIntent.putExtra("duplicate", false);// 经测试不是根据快捷方式的名字判断重复的
// 应该是根据快链的Intent来判断是否重复的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
// 但是名称不同时,虽然有的手机系统会显示Toast提示重复,仍然会建立快链
// 屏幕上没有空间时会提示
// 注意:重复创建的行为MIUI和三星手机上不太一样,小米上似乎不能重复创建快捷方式
// 名字
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "网络设置");
// 图标
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_perm_data_setting_black_24dp));
// 设置关联程序
Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//设置网络页面intent
// 设置关联程序
// Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
// launcherIntent.setClass(MainActivity.this, MainActivity.class);
// launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
// 发送广播
sendBroadcast(addShortcutIntent);
安卓O:
ShortcutManager scm = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//设置网络页面intent
ShortcutInfo si = new ShortcutInfo.Builder(this, "dataroam")
.setIcon(Icon.createWithResource(this, R.drawable.ic_perm_data_setting_black_24dp))
.setShortLabel("网络设置")
.setIntent(launcherIntent)
.build();
assert scm != null;
scm.requestPinShortcut(si, null);
那如果要两者兼顾呢,则可以如下这样写:
//添加快捷方式
private void addShortcut() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager scm = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//设置网络页面intent
ShortcutInfo si = new ShortcutInfo.Builder(this, "dataroam")
.setIcon(Icon.createWithResource(this, R.drawable.ic_perm_data_setting_black_24dp))
.setShortLabel("网络设置")
.setIntent(launcherIntent)
.build();
assert scm != null;
scm.requestPinShortcut(si, null);
} else {
Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//"com.android.launcher.action.INSTALL_SHORTCUT"
// 不允许重复创建
addShortcutIntent.putExtra("duplicate", false);// 经测试不是根据快捷方式的名字判断重复的
// 应该是根据快链的Intent来判断是否重复的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
// 但是名称不同时,虽然有的手机系统会显示Toast提示重复,仍然会建立快链
// 屏幕上没有空间时会提示
// 注意:重复创建的行为MIUI和三星手机上不太一样,小米上似乎不能重复创建快捷方式
// 名字
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "网络设置");
// 图标
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_perm_data_setting_black_24dp));
// 设置关联程序
Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//设置网络页面intent
// 设置关联程序
// Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
// launcherIntent.setClass(MainActivity.this, MainActivity.class);
// launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
// 发送广播
sendBroadcast(addShortcutIntent);
}
}
来源:https://segmentfault.com/a/1190000012846604
标签:Android,O,桌面快捷
0
投稿
猜你喜欢
IDEA 单元测试报错:Class not found:xxxx springboot的解决
2022-05-11 01:11:02
Java并发编程之闭锁与栅栏的实现
2022-01-09 15:00:12
Android视频播放器屏幕左侧边随手指上下滑动亮度调节功能的原理实现
2022-09-12 23:27:10
C#编程中设置程序只可被运行一次的方法
2022-08-09 08:36:32
Android获取常用辅助方法(获取屏幕高度、宽度、密度、通知栏高度、截图)
2023-10-30 19:42:57
Android 下的 QuickJS Binding 库特性使用详解
2022-08-08 00:24:24
Springboot通过run启动web应用的方法
2021-08-30 01:57:37
Java设计模式之单例模式简介
2023-12-11 12:04:08
MyBatis 如何配置多个别名 typeAliasesPackage
2021-11-16 06:35:54
Android Scroll实现弹性滑动_列表下拉弹性滑动的示例代码
2022-02-07 16:01:49
Java+element实现excel的导入和导出
2022-07-31 12:45:53
C#线程倒计时器源码分享
2023-08-16 07:23:36
java并发编程专题(八)----(JUC)实例讲解CountDownLatch
2021-12-19 16:43:22
通过实例深入了解java序列化
2022-12-01 17:37:39
C#写差异文件备份工具的示例
2022-02-21 02:14:00
Eclipse git推送上传错误问题解决方案
2022-07-19 15:19:39
SpringBoot实现Excel文件批量上传导入数据库
2023-11-29 11:13:44
使用java代码获取新浪微博应用的access token代码实例
2023-12-01 20:18:30
Java DOM4J方式生成XML的方法
2022-07-19 02:32:42
利用Kotlin + Spring Boot实现后端开发
2022-02-20 05:30:19