如何在Android的主屏幕上长按时以编程方式显示打开的菜单?

问题描述 投票:0回答:1

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

this menu

我只能显示带代码的主屏幕。

            Intent intent = new Intent();

            intent.setAction(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
android android-intent homescreen
1个回答
0
投票

为父级布局提供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将此布局设置为活动的小部件在这里检查

 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;
        }
    });

每次屏幕长按一次,只需检查一下即可使用

© www.soinside.com 2019 - 2024. All rights reserved.