AlarmManager - exemplo simples

Top  Previous  Next

Tela

 
Para funcionar mesmo que fechar o programa, basta não programar o onDestroy.
 

AlarmesActivity.java

 
package com.alarmes;

 

import java.util.Calendar;

 

import android.app.Activity;

import android.app.AlarmManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class AlarmesActivity extends Activity implements OnClickListener {

 

 private Button button1;

 

 @Override

 public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.main);

         button1 = (Button) findViewById(R.id.button1);

         button1.setOnClickListener(this);

 }

 

 public void onClick(View arg0) {

 

         // primeiro cria a intenção

         Intent it = new Intent("EXECUTAR_ALARME");

         PendingIntent p = PendingIntent.getBroadcast(AlarmesActivity.this, 0, it, 0);

 

         // precisamos pegar agora + 10segundos

         Calendar c = Calendar.getInstance();

         c.setTimeInMillis(System.currentTimeMillis());

         c.add(Calendar.SECOND, 10); // +10 segundos

 

         // agendar o alarme

         AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);

         long time = c.getTimeInMillis();

         alarme.set(AlarmManager.RTC_WAKEUP, time, p);

 

         // debug:

         Log.i("Alarme", "Alarme agendado!");

 }

 

 @Override

 protected void onDestroy() {

         super.onDestroy();

 

         Log.i("Alarme", "Alarme finalizado!");

 

         // primeiro cria a intenção

         Intent it = new Intent("EXECUTAR_ALARME");

         PendingIntent p = PendingIntent.getBroadcast(AlarmesActivity.this, 0, it, 0);

 

         // cancela o amarme

         AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);

         alarme.cancel(p);

 }

}

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:orientation="vertical" >

 

  <Button

      android:id="@+id/button1"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="Agendar alarme" />

 

</LinearLayout>

 

ReceberAlarme.java

 

package com.alarmes;

 

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;

 

public class ReceberAlarme extends BroadcastReceiver {

 

 @Override

 public void onReceive(Context c, Intent i) {

         Toast.makeText(c, "Alarme!!!!", Toast.LENGTH_SHORT).show();

 }

}

 

manifest

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

  package="com.alarmes"

  android:versionCode="1"

  android:versionName="1.0" >

 

  <uses-sdk android:minSdkVersion="7" />

 

  <application

      android:icon="@drawable/ic_launcher"

      android:label="@string/app_name" >

      <activity

          android:name=".AlarmesActivity"

          android:label="@string/app_name" >

          <intent-filter>

              <action android:name="android.intent.action.MAIN" />

              <category android:name="android.intent.category.LAUNCHER" />

          </intent-filter>

      </activity>

       

      <receiver android:name="ReceberAlarme">

          <intent-filter >

              <action android:name="EXECUTAR_ALARME" />

              <category android:name="android.intent.category.DEFAULT" />

          </intent-filter>

      </receiver>

  </application>

</manifest>