首页 > 解决方案 > 如何以编程方式显示在android主屏幕上长按时打开的菜单?

问题描述

我想从我的应用程序中显示该菜单。有可能吗?

这个菜单

我可以用代码显示到主屏幕。

            Intent intent = new Intent();

            intent.setAction(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);

标签: androidandroid-intenthomescreen

解决方案


为父布局提供一个 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;
        }
    });

每次屏幕长按时都检查一下它会起作用


推荐阅读