How to set time to silent mode

Asked

Viewed 455 times

4

I have a school schedule app and put the option to mute the device during the time stipulated by the user. However there is only the option to enable and disable silent mode, as I can do this automatically according to the selected time and time of the clock?

  • Did you make an app or are you using an app that wasn’t developed by you? It wasn’t clear to me.

  • I did, fully programmed and developed by me,! ;)

  • 1

    You can explain better, I don’t understand what’s done and what’s the problem.

  • Take a look at the class Alarmmanager, It allows you to schedule tasks to be performed at some point in the future, even if your application is no longer running and the device is not "awake". You will need an intention to put the phone in silent mode and another to return it to the previous setting.

  • @Anthonyaccioly You mean interaction instead of intention?

  • I meant the intention (of Intent, a concept of Android). I should have explained better but sleep is not leaving hehehe. If no one elaborate tomorrow I do it.

  • Ah, right. I don’t know anything about Android.

  • Your question then is: how to programmatically enable and disable silent mode?

  • From what I understand it’s just a timer from the hours, no?

  • 1

    @Felipe, the Timer is a good option for things that will run frequently while the application is open; to schedule sporadic tasks in the background AlarmManager is more recommended. See that article (in English). With the author of the article I have had problems with resource consumption and battery due to incorrect use of Timer.

Show 5 more comments

1 answer

2


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

Alarmmanager

Below is an explanatory passage of an example, to see the full example go to: http://www.techrepublic.com/blog/software-engineer/use-androids-alarmmanager-to-schedule-an-event/


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 in the Activity onCreate:

protected void onCreate(Bundle savedInstanceState) {
    ...
    setup();
    ...
}

Set the Alarmmanager in the System of your button:

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

Follow for the full example in the link:

http://www.techrepublic.com/blog/software-engineer/use-androids-alarmmanager-to-schedule-an-event/

  • 1

    I could not have written better. Only what I have to complete is that there are several other types of alarm (for example RTC_WAKEUP allows you to execute an alarm at a certain time even if the device is sleeping). Thus, you can go far beyond the "put to silent mode in 20 seconds". You can for example build an alarm to put the phone in silent mode as X hours and another to return it to the previous mode Y time later. You can even apply repetitions (of Z in Z time).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.