alarm

来源:互联网 发布:手机紫光灯软件 编辑:程序博客网 时间:2024/05/21 08:58

这是第一篇分享的博客,没什么经验比较凌乱,万见解

开发至今,看了很多博客,基本都是复制,粘贴,然后封装个库包成JAR发表就成大神,表示无语了


开始正题

安卓系统闹钟

AlarmManager
获取方式通过context获得系统服务获得
context.getSystemService(Context.ALARM_SERVICE);
很好记!希望程序员们不要每次用到去百度COPY出来
context理解为系统的文本。
后面的语句理解为:或者系统服务
输入的参数:为Context.AlARM_SERVICE翻译意思,系统文本闹钟服务
然后我们点击底层可以看到
public static final String ALARM_SERVICE = "alarm";
所以你输入"alarm"也是一样的
仔细查看API,和实践所得,系统一次只能触发一个闹钟,即为,你同时设置多个闹钟,只会触发第一个
那么有人会告诉我,重复闹钟怎么回事,系统不是有这个方法吗!
不急哥后面教写
通过上述,既然一次只能执行一个,并且为全局公用的系统工具,我们可以设计成单例模式
(请原谅我给我的闹钟取名叫桃子闹钟)!
public class TaoZiAlarm {
private static TaoZiAlarm taoZiAlarm;private AlarmManager alarmManager;private PendingIntent pendingIntent;private Context context;private TaoZiAlarm() {    // ①获取AlarmManager对象:    this.context = MyApplication.getContext();    alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);}public static TaoZiAlarm getInstance() {    if (taoZiAlarm == null) {        taoZiAlarm = new TaoZiAlarm();    }    return taoZiAlarm;}
}
现在我们需要写闹钟的启动了
查看API或者系统源码我们可以发现有以下几种启动方式
//重复闹钟。第一个参数为闹钟类型,启动时间,间隔时间,启动意图
public void setRepeating(int type, long triggerAtMillis,        long intervalMillis, PendingIntent operation) {    setImpl(type, triggerAtMillis, legacyExactLength(), intervalMillis, 0, operation,            null, null, null, null, null);}
//常规启动。第一个为闹钟类型,第二个启动时间,第三个,启动意图
public void set(int type, long triggerAtMillis, PendingIntent operation) {    setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, operation, null, null,            null, null, null);}
//准确启动,和常规启动一样的参数列表
public void setExact(int type, long triggerAtMillis, PendingIntent operation) {    setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, operation, null, null, null,            null, null);}
//自定义启动可为准确启动
public void setWindow(int type, long windowStartMillis, long windowLengthMillis,
        PendingIntent operation) {    setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, operation,            null, null, null, null, null);}
setWindow,
setExact为API19之后新增,理由,19之后系统资源为了省电,导致闹钟启动不准确,最长可到2分钟,这怎么可以忍受
对边这两个底部的执行方法,可以发现
setExact方法把
windowStartMillis默认传值为
WINDOW_EXACT而已
就是这么简单你发现了大秘密


喵!
再来理解系统闹钟,种类,API原文
ELAPSED_REALTIME 相对系统启动之后的时间设置的

Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep).

ELAPSED_REALTIME_WAKEUP 相对时间,系统处于休眠状态也可以用

Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep), which will wake up the device when it goes off.

RTC  绝对时间,系统是啥时候就啥时候启动

Alarm time in System.currentTimeMillis() (wall clock time in UTC).

RTC_WAKEUP 绝对时间休眠时间也可以用

Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off.

发现真理之后,我们可以发现,相对系统时间启动,完全没卵用啊,需求用不到啊
PASS
绝对时间好,俺们设计就要设计啥时候都听指挥的儿子!不对是代码!所以封装直接选择 你猜,好了,闹钟类就这么愉快的完成了
public class TaoZiAlarm {    private static TaoZiAlarm taoZiAlarm;    private AlarmManager alarmManager;    private PendingIntent pendingIntent;    private Context context;    private TaoZiAlarm() {        // ①获取AlarmManager对象:        this.context = MyApplication.getContext();        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);    }    public static TaoZiAlarm getInstance() {        if (taoZiAlarm == null) {            taoZiAlarm = new TaoZiAlarm();        }        return taoZiAlarm;    }
//设置闹钟,传值为输入多少秒之后启动闹钟    public void setAlarm(long setTime) {        Intent intent = new Intent();        intent.setClass(context, AlarmReceiver.class);        intent.putExtra("msg", "啊啊给我一瓶矿泉水" + setTime);        pendingIntent = null;        pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);        alarmManager.set(AlarmManager.RTC_WAKEUP, TimeUtil.getNowTimeLong() + setTime, pendingIntent);    }
//取消闹钟    public void cancelAlarm() {        if (pendingIntent != null)            alarmManager.cancel(pendingIntent);    }}
//接收端
public class AlarmReceiver extends BroadcastReceiver {    int count=0;    @Override    public void onReceive(Context context, Intent intent) {        count++;        String msg = intent.getStringExtra("msg");        long time = System.currentTimeMillis();        Toast.makeText(context, count+msg, Toast.LENGTH_SHORT).show();        TaoZiAlarm.getInstance().setAlarm(3000);        LG.i("", "------------闹钟启动时间为" + time);//        MediaPlayer mp = new MediaPlayer();        EventBus.getDefault().post(new MessageEvent(2, "0"));    }}
配置文件注册接收接收器
<receiver android:name=".alarm.AlarmReceiver"></receiver>
原创,转请标明,后续会写个工具类,控制闹钟,监听各种状态


原创粉丝点击