How to set the time for silent mode

Posted on

Question :

I have a school time app and have the option to silence the device for the time stipulated by the user. However there is only the option to enable and disable the silent mode, how can I do this automatically according to the selected time and the clock time?


Answer :

If your problem is scheduling activities on Android the best class is


Below is an explanatory part of an example,
to see the complete example:

In your Activity, declare the following class variables:

PendingIntent pi;
BroadcastReceiver br;
AlarmManager am;
AudioManager audio;

Create a private method to configure your scheduler. Instantiate the variables and set the broadcast receiver that will receive the callback when the event happens.

private void setup() {
      br = new BroadcastReceiver() {
             public void onReceive(Context c, Intent i) {
                    // Escreva aqui o que deve ser executando quando o evento acontecer
                    Toast.makeText(c, "Entrando em modo silencioso!", Toast.LENGTH_LONG).show();
                    //Modo Silencioso
                    audio = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
      registerReceiver(br, new IntentFilter("com.seuprojeto.suaactivity") );
      pi = PendingIntent.getBroadcast( this, 0, new Intent("com.seuprojeto.suaactivity"),
0 );
      am = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));

Call the setup method on Activity’s onCreate:

protected void onCreate(Bundle savedInstanceState) {

Set AlarmManager on your button’s listener:

public void onClick(View v) {
    long seuTempo = 1000 * 20; // 20 segundos
    am.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + seuTempo, pi );

Go to the full example in the link:



Leave a Reply

Your email address will not be published. Required fields are marked *