写一个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代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:gravity="center_vertical"  
  7.     >  
  8. <Button    
  9.     android:id="@+id/timeBtn"  
  10.     android:layout_width="fill_parent"   
  11.     android:layout_height="wrap_content"   
  12.     android:text="@string/time"  
  13.     android:textSize="20sp"  
  14.     />  
  15. <Button    
  16.     android:id="@+id/cancelAlarmBtn"  
  17.     android:layout_width="fill_parent"   
  18.     android:layout_height="wrap_content"   
  19.     android:text="@string/cancelAlarm"  
  20.     />      
  21. </LinearLayout>  

 

 界面的效果如下:


3、修改Alarm.java这个activity,在该Activity中需要做这样几件事:

 

  • 获取界面上的两个按钮组件,并给其绑定事件监听器
  • 第一个时间按钮,点击后,显示时间选择对话框(TimePicker),供选择小时和分钟,并设置闹钟
  • 第二个按钮,点击之后需要当前设定的闹钟
比较难写的代码就是闹钟设置:
//设置时间
Java代码   收藏代码
  1.      timeBtn.setOnClickListener(new Button.OnClickListener(){  
  2. @Override  
  3. public void onClick(View arg0) {  
  4.     Log.d(TAG, "click the time button to set time");  
  5.     calendar.setTimeInMillis(System.currentTimeMillis());  
  6.     new TimePickerDialog(Alarm.this,new TimePickerDialog.OnTimeSetListener() {  
  7.         @Override  
  8.         public void onTimeSet(TimePicker arg0, int h, int m) {  
  9.             //更新按钮上的时间  
  10.             timeBtn.setText(formatTime(h,m));  
  11.             //设置日历的时间,主要是让日历的年月日和当前同步  
  12.             calendar.setTimeInMillis(System.currentTimeMillis());  
  13.             //设置日历的小时和分钟  
  14.             calendar.set(Calendar.HOUR_OF_DAY, h);  
  15.             calendar.set(Calendar.MINUTE, m);  
  16.             //将秒和毫秒设置为0  
  17.             calendar.set(Calendar.SECOND, 0);  
  18.             calendar.set(Calendar.MILLISECOND, 0);  
  19.             //建立Intent和PendingIntent来调用闹钟管理器  
  20.             Intent intent = new Intent(Alarm.this,AlarmReceiver.class);  
  21.             PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarm.this0, intent, 0);  
  22.             //获取闹钟管理器  
  23.             AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);  
  24.             //设置闹钟  
  25.             alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);  
  26.             alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);  
  27.             Toast.makeText(Alarm.this"设置闹钟的时间为:"+String.valueOf(h)+":"+String.valueOf(m), Toast.LENGTH_SHORT).show();  
  28.             Log.d(TAG, "set the time to "+formatTime(h,m));  
  29.         }  
  30.     },calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show();                  
  31. }  
  32.      });  

  代码里面有注释,这里就不多解释了,其中new TimePickerDialog为创建时间选择对话框。为了能够看到效果,我给闹钟添加了重复提醒:alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);

还要为取消闹钟按钮添加事件监听器:

 

Java代码   收藏代码
  1. //取消闹钟按钮事件监听  
  2.         final Button cancelAlarmBtn = (Button)findViewById(R.id.cancelAlarmBtn);  
  3.         cancelAlarmBtn.setOnClickListener(new Button.OnClickListener(){  
  4.             @Override  
  5.             public void onClick(View arg0) {  
  6.                 Intent intent = new Intent(Alarm.this,AlarmReceiver.class);  
  7.                 PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarm.this0, intent, 0);  
  8.                 //获取闹钟管理器  
  9.                 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);  
  10.                 alarmManager.cancel(pendingIntent);  
  11.                 Toast.makeText(Alarm.this"闹钟已经取消!", Toast.LENGTH_SHORT).show();  
  12.             }  
  13.         });  

  在点击取消闹钟按钮时,取消之前设置的闹钟,核心代码就4行。

 

 

4、编写广播接收器,用来接收闹钟的广播事件,然后进行相关处理,

 

Java代码   收藏代码
  1. public class AlarmReceiver extends BroadcastReceiver {  
  2.   
  3.     /* (non-Javadoc) 
  4.      * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent) 
  5.      */  
  6.     @Override  
  7.     public void onReceive(Context arg0, Intent data) {  
  8.         Log.d(Alarm.TAG, "the time is up,start the alarm...");  
  9.         Toast.makeText(arg0, "闹钟时间到了!", Toast.LENGTH_SHORT).show();  
  10.     }  
  11. }  

 

这个代码就很简单了,主要是要继 承 BroadcastReceiver 这个类,然后重写onRecive方法。onRecive方法在闹钟的时间达到之后会执行,在这里我们可以做自己的事情,比如启动某个程序,或者播放铃声,我这里就是简单的提示一下,使用的是Toast。

 

5、在android的AndroidManifest.xml文件中注册广播接收器:

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

Xml代码   收藏代码
  1.       package="com.ql.activity"  
  2.       android:versionCode="1"  
  3.       android:versionName="1.0">  
  4.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  5.          <receiver android:name=".AlarmReceiver" android:process=":remote" />  
  6.         <activity android:name=".Alarm"  
  7.                   android:label="@string/app_name">  
  8.             <intent-filter>  
  9.                 <action android:name="android.intent.action.MAIN" />  
  10.                 <category android:name="android.intent.category.LAUNCHER" />  
  11.             </intent-filter>  
  12.         </activity>  
  13.   
  14.     </application>  
  15.     <uses-sdk android:minSdkVersion="8" />  
  16. </manifest>   

  核心的配置为<receiver android:name=".AlarmReceiver" android:process=":remote" />,这也是闹钟程序的关键,如果不做这个配置,那么时间到了之后,闹钟将不会提示。

 

到此为止,我们的小闹钟程序就结束了,接下来就是到模拟器上测试,运行截图如上图。程序源代码见附件。

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值