listen volume button when phone is locked android
Asked Answered
D

2

8

I have a service in my application that run on backend, the service can be started when I press button volume up and stopped when I press button volume down.

public class SettingsContentObserver extends ContentObserver {


int previousVolume;
Context context;

public SettingsContentObserver(Context c, Handler handler) {
    super(handler);
    context=c;

    AudioManager audio = (AudioManager)        context.getSystemService(Context.AUDIO_SERVICE);
    previousVolume = audio.getStreamVolume(AudioManager.STREAM_RING);

}



@Override
public boolean deliverSelfNotifications() {
    return super.deliverSelfNotifications();
}

@Override
public void onChange(boolean selfChange) {
    super.onChange(selfChange);

    AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);

    int delta=previousVolume-currentVolume;



        if(delta > 0)
        {
            System.out.println("Decreased") ;
            Intent intent = new Intent(context, MyAlarmService.class);
            context.stopService(intent);
            previousVolume=currentVolume;
        }
        else if(delta < 0)
        {
              System.out.println("Increased");
              Intent intent = new Intent(context, MyAlarmService.class);
              context.startService(intent);
              previousVolume=currentVolume;
        }


}
}

I want to do the same things when my phone is locked (when the screen is OFF), when I search in the net I find that I must use a BroadcastReceivre, I test it but it doesn’t work.

public class YourBoardcastReceiver extends BroadcastReceiver {
    @Override
public void onReceive(Context context, Intent intent) {


    Log.e("get something", "i dont know what!!");

    String intentAction = intent.getAction();
    KeyEvent event = null;
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        event = (KeyEvent) intent
            .getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    }

    if (event == null) {
        return;
    }

    int keycode = event.getKeyCode();
    int action = event.getAction();
    long eventtime = event.getEventTime();

    if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
        || keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
    if (action == KeyEvent.ACTION_DOWN) {

            Intent intent1 = new Intent(context, MyAlarmService.class);
            context.stopService(intent1);

        if (isOrderedBroadcast()) {
            abortBroadcast();
        }

        else if (action == KeyEvent.ACTION_UP) {

              Intent intent11 = new Intent(context, MyAlarmService.class);
              context.startService(intent11);

        }

            if (isOrderedBroadcast()) {
                abortBroadcast();


    }
}
}
}


}

In android manifest I add :

<receiver android:name="YourBoardcastReceiver">
            <intent-filter>
                    <action android:name="android.intent.action.SCREEN_ON" />
            </intent-filter>
    </receiver>

And in the mainActivity on the methode oncrete I add:

AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
ComponentName mRemoteControlResponder = new ComponentName(getPackageName(),
YourBoardcastReceiver.class.getName());

Pleas Can I give me some help, Thinks.

Dalmatian answered 27/10, 2014 at 11:29 Comment(0)
B
7

When phone is locked/sleep mode we cannot listen to the button changes, However we can do some hack to listen to volume button.

Github: https://github.com/praslnx8/volume_Screen_Lock

Those apps are playing media with 0 volume in background and then the key will be always listened by Reciever with MediaButtonIntent

Brat answered 3/2, 2016 at 5:2 Comment(0)
T
0

Try Register with BroadCast Receiver for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON

Thyrse answered 20/8, 2015 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.