写一个android小闹钟
接触android也有一段时间了,书也看了,但从写过hello android之后,就没写过什么程序了,今天准备写个小闹钟程序。
环境搭建就不讲了,直接说开发。
小闹钟程序开发中的要点就是:
1、时间选择对话框(TimePicker)
2、获取闹钟管理器并对其进行设置
3、注册广播接收器
掌握了这两点,写程序就很简单了。
1、新建android项目:Alarm,sdk版本选择2.2,Package name:com.lql.activity,Main Activity:Alarm
2、编写界面:直接修改layout中的main.xml文件,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center_vertical"
- >
- <Button
- android:id="@+id/timeBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/time"
- android:textSize="20sp"
- />
- <Button
- android:id="@+id/cancelAlarmBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/cancelAlarm"
- />
- </LinearLayout>
界面的效果如下:
3、修改Alarm.java这个activity,在该Activity中需要做这样几件事:
- 获取界面上的两个按钮组件,并给其绑定事件监听器
- 第一个时间按钮,点击后,显示时间选择对话框(TimePicker),供选择小时和分钟,并设置闹钟
- 第二个按钮,点击之后需要当前设定的闹钟
- timeBtn.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View arg0) {
- Log.d(TAG, "click the time button to set time");
- calendar.setTimeInMillis(System.currentTimeMillis());
- new TimePickerDialog(Alarm.this,new TimePickerDialog.OnTimeSetListener() {
- @Override
- public void onTimeSet(TimePicker arg0, int h, int m) {
- //更新按钮上的时间
- timeBtn.setText(formatTime(h,m));
- //设置日历的时间,主要是让日历的年月日和当前同步
- calendar.setTimeInMillis(System.currentTimeMillis());
- //设置日历的小时和分钟
- calendar.set(Calendar.HOUR_OF_DAY, h);
- calendar.set(Calendar.MINUTE, m);
- //将秒和毫秒设置为0
- calendar.set(Calendar.SECOND, 0);
- calendar.set(Calendar.MILLISECOND, 0);
- //建立Intent和PendingIntent来调用闹钟管理器
- Intent intent = new Intent(Alarm.this,AlarmReceiver.class);
- PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarm.this, 0, intent, 0);
- //获取闹钟管理器
- AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
- //设置闹钟
- alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
- alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);
- Toast.makeText(Alarm.this, "设置闹钟的时间为:"+String.valueOf(h)+":"+String.valueOf(m), Toast.LENGTH_SHORT).show();
- Log.d(TAG, "set the time to "+formatTime(h,m));
- }
- },calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show();
- }
- });
代码里面有注释,这里就不多解释了,其中new TimePickerDialog为创建时间选择对话框。为了能够看到效果,我给闹钟添加了重复提醒:alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);。
还要为取消闹钟按钮添加事件监听器:
- //取消闹钟按钮事件监听
- final Button cancelAlarmBtn = (Button)findViewById(R.id.cancelAlarmBtn);
- cancelAlarmBtn.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View arg0) {
- Intent intent = new Intent(Alarm.this,AlarmReceiver.class);
- PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarm.this, 0, intent, 0);
- //获取闹钟管理器
- AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
- alarmManager.cancel(pendingIntent);
- Toast.makeText(Alarm.this, "闹钟已经取消!", Toast.LENGTH_SHORT).show();
- }
- });
在点击取消闹钟按钮时,取消之前设置的闹钟,核心代码就4行。
4、编写广播接收器,用来接收闹钟的广播事件,然后进行相关处理,
- public class AlarmReceiver extends BroadcastReceiver {
- /* (non-Javadoc)
- * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
- */
- @Override
- public void onReceive(Context arg0, Intent data) {
- Log.d(Alarm.TAG, "the time is up,start the alarm...");
- Toast.makeText(arg0, "闹钟时间到了!", Toast.LENGTH_SHORT).show();
- }
- }
这个代码就很简单了,主要是要继 承 BroadcastReceiver 这个类,然后重写onRecive方法。onRecive方法在闹钟的时间达到之后会执行,在这里我们可以做自己的事情,比如启动某个程序,或者播放铃声,我这里就是简单的提示一下,使用的是Toast。
5、在android的AndroidManifest.xml文件中注册广播接收器:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.ql.activity"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <receiver android:name=".AlarmReceiver" android:process=":remote" />
- <activity android:name=".Alarm"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="8" />
- </manifest>
核心的配置为<receiver android:name=".AlarmReceiver" android:process=":remote" />,这也是闹钟程序的关键,如果不做这个配置,那么时间到了之后,闹钟将不会提示。
到此为止,我们的小闹钟程序就结束了,接下来就是到模拟器上测试,运行截图如上图。程序源代码见附件。