How to set the time for silent mode

4

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?

    
asked by anonymous 25.02.2014 / 02:06

1 answer

2

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

AlarmManager

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

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() {
             @Override
             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);
                    audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
             }
      };
      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) {
    ...
    setup();
    ...
}

Set AlarmManager on your button's listener:

@Override
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:

link

    
25.02.2014 / 23:00