Android Sleep/Standby Mode

Android Sleep/Standby Mode

I'm sure the workaround isn't too difficult

Using AlarmManager is a bit tricky.

My question is what exactly does "sleep mode" do on android systems?

Primarily, it shuts down the CPU. Along the way, non-essential radios (WiFi, GPS) will have been shut down as well.

What does it stop, what doesn't it stop, etc.

About the only thing you can count on not stopping is the GSM or CDMA radio (for incoming calls, SMSes, and IP packets) and AlarmManager.

By sleep mode I mean, of course, when you press the power button and the screen goes black.

Actually, that's not sleep mode, per se. That is the screen turning off.

The device will go into sleep mode shortly thereafter, if nothing is keeping it awake with a WakeLock. However, there is no guarantee that within a millisecond of you pressing that button and the screen turning off that the CPU is off.

What happens to Activities/Services when phone goes to sleep/standby mode?

In case of device sleep, activity's `onPause()' will be called. Read activity lifecycle to understand this.

OS only kills the process when memory/resources are low. Activities are killed first, services are only killed as last resort.

But there is no guarantee they will not be killed. This is why you should rely on system services to call you when you need some work done: use AlarmManager to do call your code periodically or use listeners to notify you of system changes (gps, network, etc..)

When does android device go to sleep mode?

You can never really be sure if the device has gone to sleep. At best, I can give you a list of things that would make sure that the device hasn't gone to sleep:

  • Screen being on - Requires CPU and GPU, and hence RAM, to stay active
  • Any app holding a wakelock - Even the lowest wakelocks keep the CPU on, though the screen may be switched off

Apart from this, there is no guarantee that the device will go to sleep n seconds after the screen is turned off, even if no wakelocks are being held. This is upto the OEM, and they can alter this.

Keep in mind that Android devices have an option to keep the device unlocked for a certain amount of time after the screen has gone to sleep.

Playing music in sleep/standby mode in Android 2.3.3

I think you should run the media in background using services
So you create a service and put your media code in it and attach it to start and stop buttons maybe somthing like this :

public class MediaPlayerService extends Service {
MediaPlayer myMediaPlayer;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
FileInputStream fis = new FileInputStream(songList.get(0));
FileDescriptor fd = fis.getFD();

if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.release();
mediaPlayer = null;
}
}

mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(fd);
mediaPlayer.prepare();
playPauseButton.setImageResource(android.R.drawable.ic_media_pause);
mediaPlayer.seekTo(songPosition);
appMsg.setText(songList.get(0));
}
@Override
public void onStart(Intent intent, int startid) {
myMediaPlayer.start();
}
@Override
public void onDestroy() {
myMediaPlayer.stop();
}
}

After that you start that service when a start button is pressed using methodestartService and stop it using methode stopService in your Activity class



Related Topics



Leave a reply



Submit