开发者

How to disable search key and camera key while displaying menu?

I've created a menu in my one activity as follows:

/menu/my_menu_list.xml

   <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/mn_about" android:title="About"
            android:icon="@drawable/about" />
        <item android:id="@+id/mn_display_list" android:title="Display List"
            android:icon="@drawable/display_list" />
    </menu>

Also overriden these methods.

 @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.my_menu_list, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.mn_about:
                startActivity(new Intent(this, About.class));
                break;

            case R.id.mn_display_list:
                startActivity(new Intent(this, DisplayList.class));
                break;
            }
        }

Those work perfect. Now I need to disable search key and camera key while displaying this menu. The following code works fine for an activity but I don't know where to put this code for menu.

 @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_CAMERA)) {
              开发者_C百科  return false;
            }
            if ((keyCode == KeyEvent.KEYCODE_SEARCH)) {
                return false;
            }
            return super.onKeyDown(keyCode, event);
        }

Edit 1

I disabled the camera for whole application. For this, I added following receiver tag in manifest file:

<receiver android:name=".MyReceiver">
            <intent-filter android:priority="10000">
                <action android:name="android.intent.action.CAMERA_BUTTON" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter android:priority="12000">
                <action android:name="android.intent.action.VOICE_COMMAND" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter android:priority="11000">
                <action android:name="android.intent.action.SEARCH_LONG_PRESS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
    </receiver>

Also created a Receiver class that extends BroadcastReceiver.

public class Receiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
    abortBroadcast();
    }
}

This solved for the camera but not working for long press search and voice dialer.

Edit 2 I also tried following way to control Long_Press_Search key. But not working.

    public class MyMenuInflater extends MenuInflater implements OnKeyListener {

        public MyMenuInflater(Context context) {
            super(context);
        }

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                Log.d("KeyPress", "search");
                return true;
            }
            return false;
        }
   }

And changed onCreateOptionsMenu() as follows.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MyMenuInflater inflater=new MyMenuInflater(this);
    inflater.inflate(R.menu.my_menu_list, menu);
    return true;
}

It does not capture the keypress event.

What do I need to change?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜