Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect volume button other way #44

Open
nexussmart opened this issue Dec 4, 2019 · 3 comments
Open

Detect volume button other way #44

nexussmart opened this issue Dec 4, 2019 · 3 comments

Comments

@nexussmart
Copy link

Hi,

Is it possible to otherwise detect volume button events on Android so you do not have to ask for permissions?

[https://github.com/manueldeveloper/cordova-plugin-volume-buttons/blob/master/src/android/VolumeButtonsListener.java](Cf plugin ionic )

@giantsol
Copy link
Member

giantsol commented Dec 4, 2019

Hi @nexussmart ,

In normal Android development(i.e. native Android), it is very easy to detect volume button events without requesting ACTION_MANAGER_OVERLAY_PERMISSION that we request here. You can just override onKeyDown method in your Activity.

However, flutter plugin environment provides very limited api, and there was no easy way to implement volume detection except what we did in this plugin. So.. as far as it seems now, ACTION_MANAGER_OVERLAY_PERMISSION is necessary.

Besides, the link you shared with us is only for Cordova, and its development environment is different from that of flutter plugin, so it couldn't be our option :(

Thanks.

@nexussmart
Copy link
Author

Hi @giantsol,

I override the mainactivity to detect event key Volume Up and Down :

`

public static final String STREAM = "com.enterprise.eventkeychannel/stream";

private EventChannel.EventSink eventSink = null;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new EventChannel(getFlutterView(), STREAM).setStreamHandler(
  new EventChannel.StreamHandler() {
      @Override
      public void onListen(Object args, final EventChannel.EventSink events) {
          eventSink = events;
      }
      @Override
      public void onCancel(Object args) {
          eventSink = null;
      }
  }
 );
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
  if(event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
    if( eventSink != null) { eventSink.success(0); }
  } else {
    if(event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
      if( eventSink != null) { eventSink.success(1); }
    }
  }
  return super.dispatchKeyEvent(event);
}`

@giantsol
Copy link
Member

giantsol commented Dec 6, 2019

Yep, that's exactly what you can do if it's your own app.
However, in our case, as we're developing a plugin, we cannot override your(or other app's) MainActivity, so we can't take the easy way that you just implemented.

You just wrote your own EventChannel!
Congratulations! 💪🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants