jueves, 22 de marzo de 2012

Como construir una alarma con android facilmente

Hola amigos,

Hoy les voy a poner un ejemplo de una activity que establece una alarma en android para que podais crearos vuestras propias alarmas facilmente:

public class AlarmController extends Activity {
Toast mToast;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.alarm_controller);

// Watch for button clicks.
Button button = (Button)findViewById(R.id.one_shot);
button.setOnClickListener(mOneShotListener);
button = (Button)findViewById(R.id.start_repeating);
button.setOnClickListener(mStartRepeatingListener);
button = (Button)findViewById(R.id.stop_repeating);
button.setOnClickListener(mStopRepeatingListener);
}

private OnClickListener mOneShotListener = new OnClickListener() {
public void onClick(View v) {
// When the alarm goes off, we want to broadcast an Intent to our
// BroadcastReceiver. Here we make an Intent with an explicit class
// name to have our own receiver (which has been published in
// AndroidManifest.xml) instantiated and called, and then create an
// IntentSender to have the intent executed as a broadcast.
Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
0, intent, 0);

// We want the alarm to go off 30 seconds from now.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);

// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

// Tell the user about what we did.
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(AlarmController.this, R.string.one_shot_scheduled,
Toast.LENGTH_LONG);
mToast.show();
}
};

private OnClickListener mStartRepeatingListener = new OnClickListener() {
public void onClick(View v) {
// When the alarm goes off, we want to broadcast an Intent to our
// BroadcastReceiver. Here we make an Intent with an explicit class
// name to have our own receiver (which has been published in
// AndroidManifest.xml) instantiated and called, and then create an
// IntentSender to have the intent executed as a broadcast.
// Note that unlike above, this IntentSender is configured to
// allow itself to be sent multiple times.
Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
0, intent, 0);

// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 15*1000;

// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 15*1000, sender);

// Tell the user about what we did.
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(AlarmController.this, R.string.repeating_scheduled,
Toast.LENGTH_LONG);
mToast.show();
}
};

private OnClickListener mStopRepeatingListener = new OnClickListener() {
public void onClick(View v) {
// Create the same intent, and thus a matching IntentSender, for
// the one that was scheduled.
Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
0, intent, 0);

// And cancel the alarm.
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);

// Tell the user about what we did.
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(AlarmController.this, R.string.repeating_unscheduled,
Toast.LENGTH_LONG);
mToast.show();
}
};
}


Saludos




4 comentarios:

  1. Saludos,

    gracias por tu aportación..

    ¿Sabrías decirme si es posible configurar Alarmmanager de modo que despierte el terminal cuando está totalmente parado?

    Un saludo y gracias.

    ResponderEliminar
  2. Le falta explicacion y clases, ademas de recursos que no especificas que son, solo los mandas llamar...
    RepeatingAlarm.class esta clase que hacer ?
    R.string.one_shot_scheduled este recurso que dice ?

    ResponderEliminar
  3. Gracias por el aporte, este par de weones quieren la wea lista, investiguen, el copiar y pegar no los ayudara en la vida laboral, imbeciles!!!!

    ResponderEliminar