You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
准备好资源,那资源怎么完成转换那?
public static void setDefaultNightMode (int mode)
这个API可以完成切换,让我再看看参数
public static final int MODE_NIGHT_AUTO
看上去是最合适的参数,自动切多好,但是啊但是
This constant is deprecated.
Use MODE_NIGHT_AUTO_TIME instead ,转方向。。。
public static final int MODE_NIGHT_AUTO_TIME
This constant is deprecated.
Automatic switching of dark/light based on the current time is deprecated. Considering using an explicit setting, or MODE_NIGHT_AUTO_BATTERY.
一样不能用了,只能设成和电源相关了
public static final int MODE_NIGHT_YES
Night mode which uses always uses a light mode, enabling notnight qualified resources regardless of the time.
public static final int MODE_NIGHT_NO
Night mode which uses always uses a dark mode, enabling night qualified resources regardless of the time.
This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future.
强大的类,和系统服务alarm services相关。可以让我们放一定自定义的计划进去,待遇和系统里的闹钟一样了
PendingIntent
A description of an Intent and target action to perform with it. Instances of this class are created with getActivity, getActivities, getBroadcast, and getService;
顾名思义,Intent我们已经很熟悉了,PendingIntent就是延时的Intent。
定时执行的内容当然也是严格限制的,不是为所欲为,你也不能真放一个炸弹进去了
一共有四种类型
需求
就是App应用需要在7:00~19:00直接把皮肤设为浅色系,在19:00到第二天的7:00皮肤设为深色系
换肤实现
Android里面的resouce体系里面有config_qualifier,
其中就有夜间模式,需要在资源管理器中配置好
public static void setDefaultNightMode (int mode)
这个API可以完成切换,让我再看看参数
public static final int MODE_NIGHT_AUTO
看上去是最合适的参数,自动切多好,但是啊但是
This constant is deprecated.
Use MODE_NIGHT_AUTO_TIME instead ,转方向。。。
public static final int MODE_NIGHT_AUTO_TIME
This constant is deprecated.
Automatic switching of dark/light based on the current time is deprecated. Considering using an explicit setting, or MODE_NIGHT_AUTO_BATTERY.
一样不能用了,只能设成和电源相关了
public static final int MODE_NIGHT_YES
Night mode which uses always uses a light mode, enabling notnight qualified resources regardless of the time.
public static final int MODE_NIGHT_NO
Night mode which uses always uses a dark mode, enabling night qualified resources regardless of the time.
看来后面两次参数没啥问题,问题就是必须自己下场动手定时设置了
寻找合适的定时装置
首先第一反应是Timer这个类型。可以说这个类型是个经典了。定期世界能触发线程执行相关代码逻辑。用这个类来实现一定可以完成需求,但是总是觉得有点不爽。
其实我需要的就是这个家伙
对就是这种定时器,当然炸弹我们不要~~~~
安卓手机里面不是有闹钟嘛,感觉和我的需求有共通点。我们可以内置一个自定义的闹钟吗?
感谢Android系统,他提供了我在windows桌面时代没法实现的精确定时执行的功能,接下来由我娓娓道来。
AlarmManager
This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future.
强大的类,和系统服务alarm services相关。可以让我们放一定自定义的计划进去,待遇和系统里的闹钟一样了
PendingIntent
A description of an Intent and target action to perform with it. Instances of this class are created with getActivity, getActivities, getBroadcast, and getService;
顾名思义,Intent我们已经很熟悉了,PendingIntent就是延时的Intent。
定时执行的内容当然也是严格限制的,不是为所欲为,你也不能真放一个炸弹进去了
一共有四种类型
执行Activity的Intent
批量执行Activity的Intent
执行Broadcast的Intent
执行Service相关的Intent
代码实现部分
App初始化阶段先发送一个Broadcast的Intent
创建SetThemeReceiver类作为广播接收器
onReceive
先接受到广播后,先设置下当前的nightMode
再调用setNextChange来创建广播定时器
setNextChange
计算出下一次切换皮肤的时间点,创建相关PendingIntent,通过alarmManager放入系统alarm service里面
系统执行PendingIntent再次触发SetThemeReceiver 的onReceive,进入下一个轮回,生生不息
PendingIntent.getBroadcast的requestCode,一定要防止冲突,因为和PendingIntent.FLAG_CANCEL_CURRENT 这个flag配套了,可能弄不好把别人的定时任务冲掉了就不好了
AlarmType
AlarmManager.RTC:硬件时间,不唤醒休眠设备;当休眠时不发起闹钟。
AlarmManager.RTC_WAKEUP:硬件时间,当闹钟发射时唤醒休眠设备;
AlarmManager.ELAPSED_REALTIME:真实时间流逝,不唤醒休眠设备;当设备休眠时不发起闹钟。
AlarmManager.ELAPSED_REALTIME_WAKEUP:真实时间流逝,当闹钟发起时唤醒手机休眠;
RTC闹钟和ELAPSED_REALTIME 最大的差别就是前者可以通过修改手机时间触发闹钟事件,
后者要通过真实时间的流逝,即使在休眠状态,时间也会被计算。
精确的闹钟的代价
精确的闹钟被不被API推荐。
如需获取这种特殊应用访问权限,请在清单中请求 SCHEDULE_EXACT_ALARM 权限
参考链接
The text was updated successfully, but these errors were encountered: