Search code examples
androidmedia-playeralarmlockscreen

Wake up phone when service call Activity and play MediaPlayer, Android


I have a service that is running on background, in some moment this service is calling a new activity RingAlarm.

Like the name say, this activity is an alarm player. So it plays a sound until the user press the button.

Everything was good, except when I try it when the screen was lock. Then I discovered that the sound was not playing.

To take the screen and unlock it I'm using this:

    PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
    wakeLock.acquire();
    KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); 
    KeyguardLock keyguardLock =  keyguardManager.newKeyguardLock("TAG");
    keyguardLock.disableKeyguard();

And to play the ringtoon I use this:

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
     if(alert == null){
         // alert is null, using backup
         alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
         if(alert == null){  // I can't see this ever being null (as always have a default notification) but just incase
             // alert backup is null, using 2nd backup
             alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);               
         }
     }
     mPlayer = new MediaPlayer();
     try {
         mPlayer.setDataSource(this, alert);
         mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
         mPlayer.setLooping(true);
         mPlayer.prepare();
         mPlayer.start();
     }
     catch(Exception e) {
     //TODO : Implement Error Checking
         e.printStackTrace();
         Log.e("MediaPlayer", "Error while playing!");
     }

Maybe I'm doing something wrong... any idea?

Update (error)

10-28 17:49:59.562: I/MediaPlayer(3193): it is a Ringtone type is : 4
10-28 17:49:59.820: E/RingtoneManager(3193): getActualDefaultRingtoneUri : content://media/internal/audio/media/60
10-28 17:49:59.820: E/RingtoneManager(3193): Uri.parse(uriString) : content://media/internal/audio/media/60
10-28 17:49:59.828: I/MediaPlayer(3193): It is a Not a DRM RingTone: return NULl
10-28 17:49:59.828: I/MediaPlayer(3193): path is null

I still have the problem. I'm really stuck with that. I figured out that the problem is not the media player, the problem is that when the device is in lock mode, when the application start again it didn't do it properly, cause the vibration is not working either.

I'm getting this warnings in the logcat:

10-29 18:10:07.851: W/IInputConnectionWrapper(19683): getExtractedText on inactive InputConnection
10-29 18:10:07.851: W/IInputConnectionWrapper(19683): getTextBeforeCursor on inactive InputConnection
10-29 18:10:07.867: W/IInputConnectionWrapper(19683): getExtractedText on inactive InputConnection
10-29 18:10:07.867: W/IInputConnectionWrapper(19683): getTextBeforeCursor on inactive InputConnection
10-29 18:10:07.867: W/IInputConnectionWrapper(19683): getSelectedText on inactive InputConnection
10-29 18:10:07.875: W/IInputConnectionWrapper(19683): getTextAfterCursor on inactive InputConnection
10-29 18:10:07.875: W/IInputConnectionWrapper(19683): getExtractedText on inactive InputConnection
10-29 18:10:07.875: W/IInputConnectionWrapper(19683): getTextBeforeCursor on inactive InputConnection
10-29 18:10:07.875: W/IInputConnectionWrapper(19683): getSelectedText on inactive InputConnection
10-29 18:10:07.875: W/IInputConnectionWrapper(19683): getTextAfterCursor on inactive InputConnection
10-29 18:10:07.882: W/IInputConnectionWrapper(19683): beginBatchEdit on inactive InputConnection
10-29 18:10:07.882: W/IInputConnectionWrapper(19683): endBatchEdit on inactive InputConnection
10-29 18:10:07.882: W/IInputConnectionWrapper(19683): getExtractedText on inactive InputConnection
10-29 18:10:07.882: W/IInputConnectionWrapper(19683): getTextBeforeCursor on inactive InputConnection
10-29 18:10:07.882: W/IInputConnectionWrapper(19683): getSelectedText on inactive InputConnection
10-29 18:10:07.882: W/IInputConnectionWrapper(19683): getTextAfterCursor on inactive InputConnection
10-29 18:10:07.898: W/IInputConnectionWrapper(19683): beginBatchEdit on inactive InputConnection
10-29 18:10:07.898: W/IInputConnectionWrapper(19683): endBatchEdit on inactive InputConnection
10-29 18:10:07.898: W/IInputConnectionWrapper(19683): getExtractedText on inactive InputConnection
10-29 18:10:07.898: W/IInputConnectionWrapper(19683): getTextBeforeCursor on inactive InputConnection
10-29 18:10:07.898: W/IInputConnectionWrapper(19683): getSelectedText on inactive InputConnection
10-29 18:10:07.898: W/IInputConnectionWrapper(19683): getTextAfterCursor on inactive InputConnection

Any idea?

thanks


Solution

  • Solved!

    If the screen is not on when mediaplayer or vibration is called, then seams some error happen and it dosen't sound or vibrate.

    So what I did is make an asynctask where waits until the screen is turned on. When this happend the ring and vibration is called.

    And it works!

    private class AlarmTask extends AsyncTask<Void, Void, Void> {
    
        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            while (!window.isActive()) {}
            if (Preference.readBoolean(getApplicationContext(), Preference.VIBRATION, true))
                vibrate();
            if (Preference.readBoolean(getApplicationContext(), Preference.SOUND, true))
                ring();
    
            return null;
        }
    
    }