java - 在android O中固定快捷方式时接收回调/广播
问题描述
我正在尝试一次添加两个快捷方式。但是 android 8 显示了一个对话框并请求用户允许添加快捷方式,并且其中两个对话框不能同时出现。
所以我需要一种方法来在第一个对话框关闭时获得回调或广播,以便之后可以添加另一个快捷方式。目前,当用户允许请求时,我能够获得广播。但我想知道用户是否取消了对话框。
我正在使用以下代码:
@RequiresApi(api = Build.VERSION_CODES.O)
private static void addShortcutNew(Context context, Intent shortcutIntent, String label, int iconRes, int color) {
ShortcutManager manager = context.getSystemService(ShortcutManager.class);
if (manager != null && manager.isRequestPinShortcutSupported()) {
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(context, label)
.setIcon(prepareIcon(context, iconRes, color)) // prepareIcon is my own method which returns an Icon.
.setIntent(shortcutIntent)
.setShortLabel(label)
.setLongLabel(label)
.build();
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast", Toast.LENGTH_SHORT).show();
context.unregisterReceiver(this);
}
}, new IntentFilter("test_action"));
Intent intent = new Intent("test_action");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 123, intent, 0);
manager.requestPinShortcut(shortcutInfo, pendingIntent.getIntentSender());
} else {
Toast.makeText(context, R.string.add_shortcut_error, Toast.LENGTH_SHORT).show();
}
}
解决方案
推荐阅读
- c++ - Ceres-Solver:有没有办法让 Jet 在成本函数中翻倍?
- asp.net-core - ASP.NET Core DDD Update domain model using the request ViewModel
- java - 将 SQL 查询结果中的普通列表转换为嵌套列表
- regex - 我在尸检中找不到正确的正则表达式
- visual-studio-2019 - 为什么当我调用函数时,作为 Eigen::Matrx 函数输入的地址会改变?
- python - 如何在python中生成所有具有固定长度的四位数字的序列号?
- azure-storage - 仅当 Blob 完全提交时才发生 BlobCreated 事件
- r - 基于多列重塑数据集
- rest - 无法使用标头感染 api
- three.js - Three.js 网格的天使调整