4

The device is not plugged into cable for charging The device is set to sleep after 15s of inactivity

The weird outcome is that the vibrator keeps running every 60s when I set i to 60 here is my code for alarm.

public void startAlert(View view) {
        EditText text = (EditText) findViewById(R.id.time);
        int i = Integer.parseInt(text.getText().toString());
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), i*1000, pendingIntent);

        Toast.makeText(this, "Alarm set in " + i + " seconds",
                Toast.LENGTH_LONG).show();
    }

The BroadcastReceiver

public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Time is up!!!!.",
                Toast.LENGTH_LONG).show();
        // Vibrate the mobile phone
        Vibrator vibrator = (Vibrator) context
                .getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(1000);
    }
anuj
  • 1,010
  • 2
  • 11
  • 26

1 Answers1

6

You are confusing "sleep" with screen-off. You can't tell the device when to go into deep sleep mode.

As long as the processor is active, RTC will work. If the device goes to sleep (after 10 - 30 minutes of inactivity, depending on the device) you'll need RTC_WAKEUP.

So in your case, you might want to cancel the alarm when the screen turns off. See How can I tell if the screen is on in android? for more info.

Update

Just try it: play some music and wait 15 seconds. Will it stop? No. -> The device is not sleeping.

You can't set the time when the device will go to sleep because the system decides that. It's actually "display sleep". Imagine you're downloading an apk from the Play store. Should the download stop just because you told the device to "sleep" after X seconds? Of course not.

The setting in the menu is called "sleep" because the average user understands it. This setting is by the way accessible via Settings.System.putInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT, <int>).

Community
  • 1
  • 1
Lovis
  • 9,513
  • 5
  • 31
  • 47
  • The device is set to sleep after 15s of inactivity in the settings menu. Do you mean this does not make the device sleep? Please provide link to any documentation which supports this statement – anuj Mar 27 '14 at 11:36
  • @anuj I don't have a doc link, its just experience. The best I have is my updated answer and the Documentation to PowerManager and WakeLock. – Lovis Mar 27 '14 at 13:14