1

I saw this topic

Listen to volume buttons in background service?

and know the way to listenting to volume keys in background service is to detect the amount of system volume(or music...) and see if it decrease or increase we can detect buttons

i see an app that detects pressing volume up/down button together in locked mode or screen off and it is using AccessibilityService. how it is possible?

https://play.google.com/store/apps/details?id=in.blogspot.anselmbros.torchie&hl=en

Community
  • 1
  • 1
Ahmad
  • 13
  • 1
  • 5

1 Answers1

1

You can create BroadcastReceiver inside Service.

public class MyService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        final BroadcastReceiver vReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                 //your code here
            }
        };
        registerReceiver(vReceiver, new IntentFilter("android.media.VOLUME_CHANGED_ACTION"));
    }
}

Or playing media with 0 volume in background and then the key will be always listened by Reciever with MediaButtonIntent as shown in this GitHub sample

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
Magesh Pandian
  • 8,789
  • 12
  • 45
  • 60