`
aswang
  • 浏览: 839676 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

写一个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" />,这也是闹钟程序的关键,如果不做这个配置,那么时间到了之后,闹钟将不会提示。

 

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

 



 

  • 大小: 16.7 KB
13
0
分享到:
评论
16 楼 柳弯弯 2016-05-09  
用了你的源码,为什么不响啊
15 楼 bruceanne 2014-04-30  
大神,这个formatTime(h,m)方法内容是什么、
14 楼 yancewong 2014-01-03  
为什么你的Receiver没有设置IntentFilter
13 楼 webkkk 2013-04-12  
你好,问下,关于闹钟,我看大部分都是用Receiver去处理的,
如:PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarm.this, 0, intent, 0); 
但不是也可以用activity去处理吗。

这两点有什么区别啊,我感觉用activity处理能更方便些,为什么网上例子都是用Receiver处理的呢。。。
12 楼 aswang 2013-04-09  
肥肥泡泡鱼 写道
请问 如果现在4点,闹钟设置3点 设置成功后,会马上闹铃,这人怎么解决呢??


如果时间晚于现在的时间,那就把时间设置为第二天这个时间。
11 楼 肥肥泡泡鱼 2013-04-08  
请问 如果现在4点,闹钟设置3点 设置成功后,会马上闹铃,这人怎么解决呢??
10 楼 zhujinyuan 2013-01-27  
简单明了,不错哦。
9 楼 aswang 2012-09-27  
wilsonchen 写道
您好,我想问一个问题:我是在Recevier中开启了闹钟服务,让它开机启动的。
现在我想通过界面去取消以及更新闹钟闹钟的时间应该怎样做呢?


跟设置闹钟方式一样,也可以先取消了 再添加
8 楼 wilsonchen 2012-09-26  
您好,我想问一个问题:我是在Recevier中开启了闹钟服务,让它开机启动的。
现在我想通过界面去取消以及更新闹钟闹钟的时间应该怎样做呢?
7 楼 www_JE 2012-02-23  
aswang 写道
www_JE 写道
请教LZ 加了android:process=":remote"和没加的接收器到底有什么区别啊
开机广播接收器为什么就不用加呢?


加上这个是指明这个receiver将在一个新进程中运行,有些应用需要receiver一直处于运行状态,那就需要加上这个。

如果是android:process="remote",没有分号,则表示创建一个全局的进程,不同的应用共享这个进程。

如果不加,那就应该是使用应用自身的进程来处理。

感谢楼主的解答~~!!
6 楼 aswang 2012-02-21  
www_JE 写道
请教LZ 加了android:process=":remote"和没加的接收器到底有什么区别啊
开机广播接收器为什么就不用加呢?


加上这个是指明这个receiver将在一个新进程中运行,有些应用需要receiver一直处于运行状态,那就需要加上这个。

如果是android:process="remote",没有分号,则表示创建一个全局的进程,不同的应用共享这个进程。

如果不加,那就应该是使用应用自身的进程来处理。
5 楼 www_JE 2012-02-21  
请教LZ 加了android:process=":remote"和没加的接收器到底有什么区别啊
开机广播接收器为什么就不用加呢?
4 楼 aswang 2011-12-12  
ismyhotG 写道
如果我想在代码中注册broadcast,那么 android:process=":remote" 该怎么用代码设置呢?谢谢。


这个之前还没接触到,你可能要查查帮助文档了!
3 楼 ismyhotG 2011-12-11  
如果我想在代码中注册broadcast,那么 android:process=":remote" 该怎么用代码设置呢?谢谢。
2 楼 aswang 2011-10-30  
manchester1878 写道
如果想设置多个提醒,在取消时如何识别取消哪一个呢,有ID之类的标识吗,谢谢


可以通过以下方法的第二个参数来识别:
public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)

你可以看看我前面那个定时切换飞行模式那个例子,里面有讲到如何设置多个闹钟,以及如何取消。
1 楼 manchester1878 2011-10-30  
如果想设置多个提醒,在取消时如何识别取消哪一个呢,有ID之类的标识吗,谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics