kamal verma
kamal verma

Reputation: 516

How to detect keyup and keydown event in service

I have a CustomView which extends LinearLayout , I am adding it to screen using WindowManager.addView() method to detect key event in my service with following layout param flags

  LinearLayout mLinear = new LinearLayout(getApplicationContext()) {
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            super.dispatchKeyEvent(event);
            Log.i(TAG, "event");

            int action = event.getAction();
            int keyCode = event.getKeyCode();
            switch (keyCode) {

                case KeyEvent.KEYCODE_VOLUME_UP:
                    Log.i("Key", "VolumeUp");

                    return true;
                case KeyEvent.KEYCODE_VOLUME_DOWN:
                   Log.i("Key", "VolumeDown");
                    return true;

                case KeyEvent.KEYCODE_BACK:
                    Log.d(TAG, " " + "KEYCODE_BACK");

                    return false;

                default:
                    return true;
            }

        }

    };

    mView = LayoutInflater.from(this).inflate(R.layout.volume_service_layout, mLinear);
    wm = (WindowManager) getSystemService(WINDOW_SERVICE);


    if (mView.getVisibility() == View.VISIBLE)
        flag =  WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING ;


    //params
    params = new WindowManager.LayoutParams(
            100,
            100,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
    wm.addView(mView, params);

}

dispatchKeyEvent() is override in customview class,but after adding it to windows back press Button not working.When I am adding WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE flag backpress button working fine,I am unable to detect keyup and keydown event.

I want to detect keyup or keydown event in service class. Please suggest any solution for this problem.

Upvotes: 1

Views: 3212

Answers (3)

Hafiz Mohammad D
Hafiz Mohammad D

Reputation: 1

To detect volume key press from service try AccessibilityService and override onkeyEvent()
then you do rest of the computation within the overridden event

Upvotes: -1

user8961497
user8961497

Reputation:

Android doesn't document APIs on interacting with volume buttons in that case. So I think.Its not possible

Upvotes: 1

Fenil Patel
Fenil Patel

Reputation: 1546

I guess you achieve this, by using with BroadcastReceiver

Try this,

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"...>
    //code, like activities, etc

    <receiver android:name="com.example.test.VolumeBroadcast" >
        <intent-filter>
           <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
</application>

Example of a receiver:

  public class VolumeBroadcast extends BroadcastReceiver{

      public void onReceive(Context context, Intent intent) {
           //check the intent something like:
           if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
              KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
              if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
                 // Handle key press.
              }
           }
      }
  }

The way you register is like that:

 AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
// Start listening for button presses
am.registerMediaButtonEventReceiver(RemoteControlReceiver); 
// Stop listening for button presses
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);

Audio Playback

Upvotes: 1

Related Questions