Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching key presses when android phone is in deep sleep mode

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);
    }
}
like image 256
Viktor Apoyan Avatar asked Nov 13 '12 07:11

Viktor Apoyan


People also ask

What does deep sleep mean Android?

Deep sleeping apps: Shows you all apps that will never run in the background. They'll only work when you open them. You can add apps to the list by tapping the + sign, selecting your desired app(s), and then tapping Add. To remove apps, tap More options (the three vertical dots), and then tap Remove apps.

What is Android sleep mode?

With Bedtime mode, formerly known as Wind Down in the Digital Wellbeing settings, your Android phone can stay dark and quiet while you sleep. While Bedtime mode is on, it uses Do Not Disturb to silence calls, texts and other notifications that might disturb your sleep.

What is sleep mode in mobile?

Android's “Bedtime” mode, previously known as “Wind Down,” uses Do Not Disturb to silence calls, texts and notifications, while grayscale fades the colors on your phone to black and white, to reduce the draw to your screen.


1 Answers

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:

  • http://android-developers.blogspot.se/2010/06/allowing-applications-to-play-nicer.html
  • http://developer.android.com/training/managing-audio/volume-playback.html
like image 67
Tobias Ritzau Avatar answered Oct 02 '22 02:10

Tobias Ritzau