您的位置:首页 > 移动开发 > Android开发

android开发步步为营之18:闹钟AlarmManager的使用

2014-09-23 17:51 387 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: