Open hicnar opened 4 years ago
Hello @hicnar ! Yeah actually, this implementation was a to-do but was delaying on it cause we thought there was no demand for this.
Since we now know you want it, we can start working on it.
However, this would only be possible on Android and not on iOS. I'll let you know when we're done with this.
Thanks!
Thanks for replying! Why is it not possible on iOS?
As far as we saw, there was no way to block default behaviors on iOS. If you know any, please let us know.
Hello @hicnar , We'll start working on it this weekend. Just to let you know :)
same issue
Same issue here does anyone have a solution for this?
@HasanEltantawy & @bahador-r
For Android, the solution is implementing onKeyUp/Down in the way shown below. There's no viable solution for iOS as Apple explicitly bans applications that attempt to intercept or amend the default hardware buttons functionality.
class MainActivity: FlutterActivity() {
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
super.onKeyDown(keyCode, event)
return true
}
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
super.onKeyUp(keyCode, event)
return true
}
}
@hicnar I tried to implement that code in MainActivity.java but the compiler throws these errors below
The code
package com.hassaneltantawy.hisnelmoslem;
import io.flutter.embedding.android.FlutterActivity;
public class MainActivity extends FlutterActivity {
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
super.onKeyDown(keyCode, event)
return true
}
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
super.onKeyUp(keyCode, event)
return true
}
}
Errors
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: ';' expected
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: invalid method declaration; return type required
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: <identifier> expected
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: ';' expected
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: illegal start of type
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: ';' expected
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: <identifier> expected
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: illegal start of type
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: <identifier> expected
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: ';' expected
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:8: error: ';' expected
super.onKeyDown(keyCode, event)
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:9: error: ';' expected
return true
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: ';' expected
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: invalid method declaration; return type required
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: <identifier> expected
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: ';' expected
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: illegal start of type
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: ';' expected
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: <identifier> expected
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: illegal start of type
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: <identifier> expected
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: ';' expected
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:13: error: ';' expected
super.onKeyUp(keyCode, event)
^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:14: error: ';' expected
return true
^
24 errors
I make it like this and it run but the problem is still there the volume go up and down wen i press it
package com.hassaneltantawy.hisnelmoslem;
import android.view.KeyEvent;
import io.flutter.embedding.android.FlutterActivity;
public class MainActivity extends FlutterActivity {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
super.onKeyUp(keyCode, event);
return true;
}
}
@HasanEltantawy My code sample was in Kotlin and you've go Java there, sorry should have mentioned it. Anyway that's pretty bizarre there should be no difference if it's Java or Kotlin. Did you try to put that code in a new project, just to explore if it works or you placed it in the project where you actually need it to work? If I were you I would create a new project with minimal amount of code and experiment on it a bit. If possible generate a new project with Android backed by Kotlin.
Ok I'll try to do that with anew project. I hope it'll work thanks @hicnar
Even with new app and also implementing kotlin code the problem is still existing. @hicnar
@HasanEltantawy I have just modified the MainActivity.kt in my little template project that I use for re-creating Flutter issues I occasionally find and report - the project is called fluttery_stuff. Go ahead, clone it and run on your Android device. I can confirm that once I tested that just a few minutes ago, the hardware buttons (volume up/down) were fully intercepted and pressing them did not result in displaying the volume bar on my Samsung S9. The class you can run is https://github.com/hicnar/fluttery_stuff/blob/main/lib/dialogs/main.dart
@hicnar On my Samsung Galaxy S7 the implementation is working, too.
Here you can see the source code of @hicnar's Android implementation (Kotlin).
Hi,
Is it possible to intercept events generated by hardware buttons completely? For example if the app is active and volume up/down button is pressed, is it possible to intercept that event in the app handle it in the app specific way and most importantly prevent it from being acted upon further by the system so that the default behaviour that actually turns the volume up/down and displays the system dialog/popup showing the volume going up/down would not happen?
Thanks in advance for your reply! K.