If your problem is scheduling activities on Android the best class is the
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:
Did you make an app or are you using an app that wasn’t developed by you? It wasn’t clear to me.
– Math
I did, fully programmed and developed by me,! ;)
– Pedro Igor
You can explain better, I don’t understand what’s done and what’s the problem.
– bfavaretto
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.
– Anthony Accioly
@Anthonyaccioly You mean interaction instead of intention?
– bfavaretto
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.
– Anthony Accioly
Ah, right. I don’t know anything about Android.
– bfavaretto
Your question then is: how to programmatically enable and disable silent mode?
– Math
From what I understand it’s just a timer from the hours, no?
– Felipe Avelar
@Felipe, the
Timer
is a good option for things that will run frequently while the application is open; to schedule sporadic tasks in the backgroundAlarmManager
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 ofTimer
.– Anthony Accioly