11

Problem Description

I am trying to write an application which will catch Volume Up/Down and also Bluetooth Headset buttons pressing when application is in

  • Background
  • Foreground
  • Background and phone is in sleeping mode (Power button is pressed)
  • Foreground and phone is in sleeping mode (Power button is pressed)

For first two points I have write some test application and everything works in my case, I send application to background and press volume up/down and my application catch this events. After I press on a power button and my phone goes to sleep mode. Then I press volume up/down buttons phone do not react to that events, and I can't see any loge in logcat, like everything stop work.

Applications which detect keypresses in sleep mode.

I think that this problem can't be solved, but then I remember that Android standard Media Player catch this events when you press volume up/down buttons while music plays. I think that this application can do that as it is system application, but then I download Winpm player and it work in a same way. Winapm catch volume up/down events when phone is in sleeping mode. And music player applications do that.

Question

I want to understand how this can be done ? How I can write a simple application which will catch volume up/down button presses. Do I need to play some music at that time, or I can do that ? I mean maybe the main reason that Winamp and other applications catch that event is that music is played at that time. Or maybe this problem can't be solved and if it can't be solved I want to know why ? I need some arguments why it can't be solved.

Source code

I have tested some application and here are the results, I have write a simple application that catch volume up and down key presses in background and foreground and also when application is in sleep mode. Everything work perfect when application was in background and forground, my test application catch events, but when I press on a power button it stop doing that, then I press start on media pleyer and it start to play some music, after I press power button again and then press volume up and doun and my application stat catching volume up and down keypresses. It mean that then music is played my application can catch that events, so my next question how I can simulate like my phone plays somem music ? may be this is solution ?

Manifest.xml

<receiver android:name="com.vito.backgroundworker.VolumeBroadcast" android:enabled="true">
    <intent-filter>
    <action android:name="android.media.VOLUME_CHANGED_ACTION" />
    </intent-filter>
</receiver>
        
<receiver android:name="RemoteControlReceiver">
    <intent-filter>
    <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>

MainActivity.java

public class MainActivity extends Activity {
    private PowerManager.WakeLock wl;
    //private BroadcastReceiver vol = new VolumeBroadcast();
    private AudioManager mAudioManager;
    private ComponentName mRemoteControlResponder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNjfdhotDimScreen");
        
        mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        mRemoteControlResponder = new ComponentName(getPackageName(),
                RemoteControlReceiver.class.getName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        mAudioManager.registerMediaButtonEventReceiver(
                mRemoteControlResponder);
        wl.release();
    }//End of onPause

    @Override
    protected void onResume() {
        super.onResume();
        wl.acquire();
    }//End of onResume
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        mAudioManager.unregisterMediaButtonEventReceiver(
                mRemoteControlResponder);
    }
}
Community
  • 1
  • 1
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147

3 Answers3

3

Take a look at registerMediaButtonEventReceiver(). You have to register your app as the receiver of these events to be able to recevive them.

Here are some more links that you should look at:

Tobias Ritzau
  • 3,327
  • 2
  • 18
  • 29
  • your answer help me partly, but I can't still catch button presses when power button is clicked (phone is in slep mode) but if I my phone plays some music in that case I can do that, plese see my updated post. Thanks. – Viktor Apoyan Nov 13 '12 at 18:33
  • I'm not sure I understand what you want to do with the power button. `ACTION_SCREEN_OFF` and `ACTION_SCREEN_ON` broadcasts, but you can only subscribe to them if your activity is resumed I think. There is also the `ACTION_USER_PRESENT` which you get when the lock screen is unlocked. Other than these I don't think you can listen to the power button. What do you want to do? I would say that you shouldn't design your application to use the power button. It's for the system, not for an app. I would be really confusing for users if you tried to change what it does. – Tobias Ritzau Nov 14 '12 at 05:42
  • I want to do following, I need that when I press Volume Up/Down buttons that events will be cached even if phone is in sleep mode. As now my application do not do anything then phone is in sleep mode. Can I explain what I want ? – Viktor Apoyan Nov 14 '12 at 05:53
  • Ok, and that does not work when the phone is in sleep? Can you listen to volume changes instead? – Tobias Ritzau Nov 14 '12 at 07:32
  • No I can't .... nut when I turn on some music on Android Music Player I can catch that eents. – Viktor Apoyan Nov 14 '12 at 07:35
  • Look at this: http://stackoverflow.com/questions/6896746/android-is-there-a-broadcast-action-for-volume-changes – Tobias Ritzau Nov 14 '12 at 08:31
  • I think this article can't help me, have you understood what I want ? The main question ? – Viktor Apoyan Nov 14 '12 at 08:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19515/discussion-between-tobias-ritzau-and-vito-brothers) – Tobias Ritzau Nov 14 '12 at 08:40
  • This is not helpful answer :( – Viktor Apoyan Nov 21 '12 at 05:29
  • Isn't your activity getting paused when the phone sleeps? I haven't done this before, but I'm almost sure you need a service to be listening for key presses. – mauro.dec Nov 21 '12 at 18:30
  • A broadcast receiver for the media keys, but you have to register for the events, and it doesn't work in this case since it should work when other media players are playing. – Tobias Ritzau Nov 21 '12 at 19:17
  • Service doesn't help, I try :( I can't understand your second comment @TobiasRitzau – Viktor Apoyan Nov 23 '12 at 05:56
1

In my experience, an app can't receive media button broadcasts when the device is asleep, even if it is a foreground service (the media button doesn't wake it up). Holding a partial wake lock prevents it sleeping so the button press is detected. I suspect your music playing app is holding a wake lock which is why it works. I would like to be proved wrong and not need the wake lock though...

Rob
  • 11
  • 3
0

I found an open source app that actually wakes phone from sleep by pressing the volume button. https://github.com/praslnx8/volume_Screen_Lock

This app uses Service to play media.

While playing media the volume button action will always sent to the respective reciever even if the phone screen is locked...

Prasanna Anbazhagan
  • 1,693
  • 1
  • 20
  • 37
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Tunaki Feb 03 '16 at 09:24