天天看點

android開發步步為營之18:鬧鐘AlarmManager的使用

public class android.app.AlarmManager

extends Object

Class Overview

This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

public final class android.app.PendingIntent

extends Object   implements Parcelable

Class Overview

A description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int); the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.

By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity). As such, you should be careful about how you build the PendingIntent: often, for example, the base Intent you supply will have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.

(1)、設定定時器

private Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(System.currentTimeMillis());

calendar.set(Calendar.YEAR, _year);

calendar.set(Calendar.MONTH, _month - 1);// 系統是0-11

calendar.set(Calendar.DAY_OF_MONTH, _day);

calendar.set(Calendar.HOUR_OF_DAY, _hour);

calendar.set(Calendar.MINUTE, _minute);

calendar.set(Calendar.SECOND, _second);

calendar.set(Calendar.MILLISECOND, 0);

  //建立一個意圖,目的地是CallAlarm廣播接收器

Intent intentalarm = new Intent(FigoMemoAddActivity.this,

CallAlarm.class);

intentalarm.putExtra("_id", id);

//建立一個待處理(将來時)的意圖

PendingIntent operation;

//待處理(将來時)的意圖,發出一個廣播,觸發意圖intentalarm的CallAlarm.java廣播接收器

operation = PendingIntent.getBroadcast(

FigoMemoAddActivity.this, Integer.parseInt(id),

intentalarm, 0);

// }

  //設定定時器(鬧鐘)

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),

operation);

(2)、廣播接收器接收鬧鐘觸發的intent

public class CallAlarm extends BroadcastReceiver {

@Override

public void onReceive(Context arg0, Intent arg1) {

String _id=arg1.getStringExtra("_id");//擷取備忘錄編号

Log.i("CallAlarm", "擷取到的_id:"+_id);

Intent intent = new Intent(arg0, AlarmAlertActivity.class);

Bundle bundle = new Bundle();

bundle.putString("STR_CALLER", "");

bundle.putString("_id", _id);//将備忘錄編号傳遞下去

intent.putExtras(bundle);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

arg0.startActivity(intent);

}

}

(3)、AndroidManifest.xml注冊

<application android:icon="@drawable/figomemo"

android:label="@string/app_name">

    <receiver android:name=".CallAlarm" android:process=":remote"></receiver>

</application>