android - 如何以编程方式显示在android主屏幕上长按时打开的菜单?
问题描述
我想从我的应用程序中显示该菜单。有可能吗?
我可以用代码显示到主屏幕。
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
解决方案
为父布局提供一个 id
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_stack"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".STACK">
</LinearLayout>
现在通过findviewbyid在活动中将此布局设置为小部件检查here
LinearLayout ll = findViewById(R.id.ll_stack);
ll.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
return false;
}
});
每次屏幕长按时都检查一下它会起作用
推荐阅读
- python-3.x - 重构一个哈希表解决 twoSum 问题
- php - 如何在 Sweetalert 中获取价值 Mysqli
- c# - 带有 Docker 连接的 ASP.NET Core 在首次加载时被拒绝
- reactjs - Codemirror - 模块解析失败:意外的令牌
- sql - 结合具有不同 WHERE 条件的两个查询
- html - HTML 元素在移动设备上无法正确对齐
- c++ - 如何修补 Eigen 3.3.7 以解决 GCC 8.2.1 错误(arm-none-eabi 8-2018q4-major 工具链)?
- mysql - mySQL关于删除级联条件
- javascript - 当 DOM 对象被隐藏时,事件监听器是否持续存在
- python - Pyspark 错误:Java 网关进程在发送其端口号之前已退出