Let me clarify. I know that I can override the back button pressed for an Activity, but I am looking for something more specific. I want to be able to run a service in the background that will catch the back button pressed event globally, and do stuff. For the best proof of concept example, next to a toast message perhaps, I would like it to block the functionality.
This is not tied to an application, nor an activity. This should be tied globally across any application, including the home screen. I want to be able to override the back button no matter where I am, and be able to block it for example. My question should not be hard to understand.
I am looking forward to accomplishing this on Android TV 9.0; I hate the way It's designed, as well as my current air mouse that sends weird commands. For what I know, it has one button to either send the home button command or back button command, but it only sends the back button command, and on long press, it restarts the whole TV. I would like to be able to read more into the air mouse and see if I can override and send some of my own commands.
I've come across hacky examples where a dummy activity would run in the foreground, but that is impractical and a non-solution for what I'm looking for. I would like to be able to override the system buttons globally before they could reach any app, the home screen, etc.
I've also come across the Accessibility Service method. I've created a service example as follows. In the manifest:
<service
android:name=".CustomAccessibilityService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
</service>
The service class:
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;
public class CustomAccessibilityService extends AccessibilityService {
@Override
protected void onServiceConnected() {
super.onServiceConnected();
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_VIEW_CLICKED | AccessibilityEvent.TYPE_VIEW_FOCUSED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
setServiceInfo(info);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
// No idea what this does...
}
@Override
public boolean onKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
// Block the back button press???? false gives the same result
return true;
}
return super.onKeyEvent(event);
}
@Override
public void onInterrupt() {
// On service unbind?
}
}
And the XML config
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
android:accessibilityFeedbackType="feedbackGeneric"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:accessibilityFlags="flagRequestFilterKeyEvents"
android:packageNames="com.example.myappname" />
After building and running for the first time. I go to Settings > Accessibility Settings on my Android TV and find my service. I turn it on, but the back button still fires, while it shouldn't.
Perhaps, this is not meant for non-system apps? Am I missing some permissions? It warns me of nothing, prints no error, doesn't crash/freeze, the back button works normally, guides me out of the app, etc. While it shouldn't.