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=;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,, 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, );
calendar.set(Calendar.MILLISECOND, );
/* 建立Intent和PendingIntent,来调用目标组件 */
Intent intent = new Intent(Activity01.this,
AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent
.getBroadcast(Activity01.this, ,
intent, );
AlarmManager am;
/* 获取闹钟管理的实例 */
am = (AlarmManager) getSystemService(ALARM_SERVICE);
/* 设置闹钟 */
am.set(AlarmManager.RTC_WAKEUP, calendar
.getTimeInMillis(), pendingIntent);
/* 设置周期闹 */
am.setRepeating(AlarmManager.RTC_WAKEUP, System
.currentTimeMillis()
+ ( * ), ( * * * ),
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, , intent, );
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() == )
s = "" + s;
return s;
}
}
代码:这里
Android 闹钟设置的更多相关文章
- Android闹钟设置的解决方案
Android设置闹钟并不像IOS那样这么简单,做过Android设置闹钟的开发者都知道里面的坑有多深.下面记录一下,我解决Android闹钟设置的解决方案. 主要问题 API19开始AlarmMan ...
- android 闹钟设置问题
Android开发中,alarmManager在5.0以上系统,启动时间设置无效的问题 做一个app,需要后台保持发送心跳包.由于锁屏后CPU休眠,导致心跳包线程被挂起,所以尝试使用alarmMana ...
- Android简单闹钟设置
利用AlarmManager实现闹钟设置 //设置本地闹钟,actiongString:闹钟标识 setLocAlarm(int week, String actionString) { Calend ...
- 关于Android中设置闹钟的相对比较完善的解决方案
我当时说承诺为大家写一个,一直没空,直到最近又有人跟我要,我决定抽时间写一个吧.确实设置闹钟是一个比较麻烦的东西.我在这里写的这个demo抽出来了封装了一个类库,大家直接调用其中的设置闹钟和取消闹钟的 ...
- 关于Android中设置闹钟的相对完善的解决方案
前些时候,有人在我「非著名程序员」微信公众号的后台问我有没有设置闹钟的demo,我当时说承诺为大家写一个,一直没空,直到最近又有人跟我要,我决定抽时间写一个吧.确实设置闹钟是一个比较麻烦的东西.我在这 ...
- Android闹钟 AlarmManager的使用
Android闹钟 AlarmManager的使用 AlarmManager介绍 AlarmManager这个类提供对系统闹钟服务的访问接口. 你可以为你的应用设定一个在未来某个时间唤醒的功能. 当闹 ...
- Android闹钟开发与展示Demo
前言: 看过了不少安卓闹钟开发的例子,都是点到为止,都不完整,这次整一个看看. 一.闹钟的设置不需要数据库,但是展示闹钟列表的时候需要,所以需要数据库: public class MySQLiteOp ...
- Android闹钟【复杂版】
最近做闹钟,所以自己写了个Demo版本,这个程序是用listview单独的类来实现的,和activity类分开来实现的!这个是用数据库进行更新的,当闹钟设置后,闹钟图片变成闹钟的样子,闹钟取消后,图片 ...
- android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动
android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动 1.先简单设置一个闹钟提醒事件: //设置闹钟 mSetting.setOnClickListener ...
随机推荐
- hibernate 超级牛x的公共类
想法,能支持in查询和 =查询的 公共方法,类似下面实现 用 泛型 实现 参数 getList(String[] params,Object[] values){} for(int i=0;i< ...
- vs2012 condition_variable notify_one 崩溃
vs2012项目中用到 condition_variable系统方法,程序运行过程过程中偶尔出现notify_one崩溃, 程序运行的服务器系统版本是windows server 2008 R2 SP ...
- 1.C#基础篇-->封装、继承和多态
面向对象三要素:封装.继承和多态.正确理解这三个要素,才能在编程中建立面向对象的思想. 1.封装使用篇 作用:好的封装增加代码的可读性,易于维护. 什么情况下使用封装,封装的原则是? 1>功能相 ...
- HDU 5294 Tricks Device 最短路+最大流
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意: 给你个无向图: 1.求最少删除几条边就能破坏节点1到节点n的最短路径, 2.最多能删除 ...
- redis.conf配置
# Redis configuration file example # Redis示例配置文件 # Note on units: when memory size is needed, it is ...
- 设计模式之代理模式(Proxy)
只能指针是代理模式的一种: 智能指针实现需要注意的问题: 1.构造函数指明显示构造. 2.拷贝构造函数,先断开前一个指针,然后用之前指针的值初始化现在的指针. 3.赋值函数需要先断开之前的指针,然后释 ...
- 7-Highcharts曲线图之分辨带
<!DOCTYPE> <html lang='en'> <head> <title>7-Highcharts曲线图之分辨带</title> ...
- VSS
A deleted file of the same name already exists in this VSS project. Do you want to recover the delet ...
- JavaScript之四种继承方式讲解
在Javascript中,所有开发者定义的类都可以作为基类,但出于安全性考虑,本地类和宿主类不能作为基类,这样可以防止公用访问编译过的浏览器级的代码,因为这些代码可以被用于恶意攻击. 选定基类后,就可 ...
- explicit构造函数的作用
explicit构造函数是用来防止隐式转换的.请看下面的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...