Android 闹钟设置
在Android中可以通过AlarmManager 来实现闹钟,AlarmManager类是专门用来设定在某个指定的时间去完成指定的事件。AlarmManager 提供了访问系统警报的服务,只要在程序中设置了警报服务,AlarmManager 就会通过onReceive()方法去执行这些事件,就算系统处于待机状态,同样不会影响运行。可以通过 Context.getSystemService 方法来获得该服务。 AlarmManager 中的方法很少。如下所示
AlarmManager 的方法
方法 说明
cancel 取消AlarmManager服务
set 设置AlarmManager服务
setInexactRepeating 设置不精确周期
setRepeating 设置精确周期
setTimeZone 设置时区
要实现闹钟,首先需要创建一个继承自 BroadcastReceiver 的类,实现 onReceive
方法来接收这个Alarm服务,然后通过建立Intent 和 PendingIntent连接来调用
Alarm组件。当我们点击“设置闹钟”按钮时,通过 TimePickerDialog 来设置时间,当时间到我们指定的时间后onReceive
方法接收Alarm服务 运行效果见下图
设置闹钟时间
时间到后的Toast提示
首先看看我们实现的接收 Alarm 服务的 AlarmReceiver类,很简单,就是在收到消息后用一个 Toast 来提示用户,代码如下:
首先,要用到闹钟,就要用到BroadcastReceiver,比如,在闹钟响时就显示一句话,那么这个类的代码如下:
package com.FeifeiSchedule.activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.Toast; /* * author:Tammy Pi * function:用于闹钟提醒的broadcase */ public class MyBroadCast extends BroadcastReceiver{ @Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub Log.i("info","进入"); Toast.makeText(arg0,"闹钟响了",Toast.LENGTH_SHORT).show(); } }
要用到Receiver,那么就需要在AndroidMainfest.xml中声明,该声明放在application中,activity后:
<!-- 设置Receiver --> <receiver android:name=".MyBroadCast" android:process=":remote"> </receiver>
然后,再查下数据库,满足条件的全部设置上闹钟:
//循环数据库,将闹钟事件放入 List<TbSchedule> alarmList = FeifeiScheduleService.getInstance(this).findAlarmSchedule(); for(int i=0;i<alarmList.size();i++){ AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(FeifeiScheduleActivity.this,MyBroadCast.class); TbSchedule tbSchedule = alarmList.get(i); PendingIntent pi = PendingIntent.getBroadcast(FeifeiScheduleActivity.this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar calendar = Calendar.getInstance(); Date date = null; try { date = new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(tbSchedule.getMydate()); TimeZone tm = TimeZone.getTimeZone("GMT"); calendar.setTimeZone(tm); calendar.clear(); calendar.setTime(date); //设置闹钟时间 alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pi); myMap.put(tbSchedule.getScheduleid(),pi); }catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i("info",e.getMessage()); } }
然后就只需要对”设置闹钟”、”取消闹钟” 的事件进行监听了,代码如下:
package com.yarin.android.Examples_07_07; import java.util.Calendar; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; public class Activity01 extends Activity { Button mButton1; Button mButton2; TextView mTextView; Calendar calendar; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); calendar = Calendar.getInstance(); mTextView = (TextView) findViewById(R.id.TextView01); mButton1 = (Button) findViewById(R.id.Button01); mButton2 = (Button) findViewById(R.id.Button02); mButton1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { calendar.setTimeInMillis(System.currentTimeMillis()); int mHour = calendar.get(Calendar.HOUR_OF_DAY); int mMinute = calendar.get(Calendar.MINUTE); new TimePickerDialog(Activity01.this, new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { calendar.setTimeInMillis(System .currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); /* 建立Intent和PendingIntent,来调用目标组件 */ Intent intent = new Intent(Activity01.this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent .getBroadcast(Activity01.this, 0, intent, 0); AlarmManager am; /* 获取闹钟管理的实例 */ am = (AlarmManager) getSystemService(ALARM_SERVICE); /* 设置闹钟 */ am.set(AlarmManager.RTC_WAKEUP, calendar .getTimeInMillis(), pendingIntent); /* 设置周期闹 */ am.setRepeating(AlarmManager.RTC_WAKEUP, System .currentTimeMillis() + (10 * 1000), (24 * 60 * 60 * 1000), pendingIntent); String tmpS = "设置闹钟时间为" + format(hourOfDay) + ":" + format(minute); mTextView.setText(tmpS); } }, mHour, mMinute, true).show(); } }); mButton2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Activity01.this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( Activity01.this, 0, intent, 0); AlarmManager am; /* 获取闹钟管理的实例 */ am = (AlarmManager) getSystemService(ALARM_SERVICE); /* 取消 */ am.cancel(pendingIntent); mTextView.setText("闹钟已取消!"); } }); } /* 格式化字符串(7:3->07:03) */ private String format(int x) { String s = "" + x; if (s.length() == 1) s = "0" + s; return s; } }
代码:这里