Android护眼功能
目前全国已有3亿多视光疾病患者,每年眼镜需求量达7000万副,近视率从过去的全球排名第四上升到第二,仅次于日本。保护眼睛成了一个刻不容缓的话题。对于很多学生、上班族来说,除了每天大量时间对着书本、电脑外,恐怕眼睛最大的焦点都是放在手机上,因此,一款能够保护眼睛的手机软件就愈发显得重要。
于是,为了保护眼睛,我便做了个应用
主要涉及到的知识点:
- Service:除了前台服务外,实际开发中Service还有一种常见的用法,就是执行定时任务, 比如轮询,就是每间隔一段时间就请求一次服务器,确认客户端状态或者进行信息更新,而Android中给我们提供的定时方式有Alarm机制!
- 定时服务AlarmManager:
set(int type,long startTime,PendingIntent pi):一次性闹钟
setRepeating(int type,long startTime,long intervalTime,PendingIntent pi): 重复性闹钟,和3有区别,3闹钟间隔时间不固定
setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi): 重复性闹钟,时间不固定
cancel(PendingIntent pi):取消AlarmManager的定时服务
getNextAlarmClock():得到下一个闹钟,返回值AlarmManager.AlarmClockInfo
setAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) 和set方法类似,这个闹钟运行在系统处于低电模式时有效
setExact(int type, long triggerAtMillis, PendingIntent operation): 在规定的时间精确的执行闹钟,比set方法设置的精度更高
setTime(long millis):设置系统墙上的时间
setTimeZone(String timeZone):设置系统持续的默认时区
setWindow(int type, long windowStartMillis, long windowLengthMillis, PendingIntent operation): 设置一个闹钟在给定的时间窗触发。类似于set,该方法允许应用程序精确地控制操作系统调 整闹钟触发时间的程度。
4.Notification的基本使用流程以及设置相关的一些方法
在AndroidManifest中设置Service以及Notification
<applicationandroid:allowBackup="true"android:icon="@drawable/mice"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme" ><activity android:name=".Activity.MainActivity" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".Activity.Activity_inform"/><activity android:name=".Activity.Activity_time"/><service android:name=".Service.LongRunningService" ></service><receiver android:name=".Service.AlarmReceiver" ></receiver></application>
设置定时服务
mainactivity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;
public void onClick(View v) {Intent intent=new Intent(MainActivity.this, LongRunningService.class);switch(v.getId()){case R.id.bt_menu://打开左边的抽屉mDrawerLayout.openDrawer(Gravity.LEFT);break;case R.id.bt_start_inform:startService(intent);//当提示开启后 “开启提示”不可点击,“关闭提示”可以点击bt_start_inform.setEnabled(false);bt_stop_inform.setEnabled(true);Toast.makeText(MainActivity.this, "提醒功能已经开启。\nAPP关闭了仍然能够提醒哦!", Toast.LENGTH_LONG).show();break;case R.id.bt_stop_inform:stopService(intent);bt_start_inform.setEnabled(true);bt_stop_inform.setEnabled(false);Toast.makeText(MainActivity.this, "提醒功能已经关闭!", Toast.LENGTH_SHORT).show();break;}}
AlarmReceiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;public class AlarmReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Intent i = new Intent(context, LongRunningService.class);context.startService(i);}}
LongRunningService:
import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import com.example.protectyoureyes.R;
import com.example.protectyoureyes.bean.GlobalData;public class LongRunningService extends Service {@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {//启用前台服务,主要是startForeground()Notification notification=new Notification.Builder(this).setContentTitle(GlobalData.inform_title).setContentText(GlobalData.inform_content).setSmallIcon(R.drawable.mice).setLargeIcon(GlobalData.inform_bitmap).build();//设置振动notification.vibrate = GlobalData.all_vibrate_type[GlobalData.vibrate_type_number];startForeground(1, notification);AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);//读者可以修改此处的Minutes从而改变提醒间隔时间//此处是设置每隔55分钟启动一次//这是55分钟的毫秒数int Minutes = GlobalData.inform_time*60*1000;//SystemClock.elapsedRealtime()表示1970年1月1日0点至今所经历的时间long triggerAtTime = SystemClock.elapsedRealtime() + Minutes;//此处设置开启AlarmReceiver这个BroadcastReceiverIntent i = new Intent(this, AlarmReceiver.class);PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);//ELAPSED_REALTIME_WAKEUP表示让定时任务的出发时间从系统开机算起,并且会唤醒CPU。manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();//在Service结束后关闭AlarmManagerAlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);Intent i = new Intent(this, AlarmReceiver.class);PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);manager.cancel(pi);}
}
Notification的使用
activity_main:
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical">
<Buttonandroid:id="@+id/bt_menu"android:layout_width="45dp"android:layout_height="45dp"android:background="@drawable/mice"/>
notification_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:textSize="24sp"android:text="这是通知点击后的界面"/></RelativeLayout>
实现
功能图片
APP图标
开启护眼功能的界面
设置完以后
到时间后的任务栏的提醒
参考资料
https://blog.csdn.net/Double2hao/article/details/49719639
https://www.runoob.com/w3cnote/android-tutorial-notification.html
Android护眼功能相关推荐
- Android护眼功能,双非本科字节跳动Android面试题分享
//打开左边的抽屉 mDrawerLayout.openDrawer(Gravity.LEFT); break; case R.id.bt_start_inform: startService(int ...
- Android护眼模式功能小记
最近自己在做一个小说阅读器,看到某阅有护眼模式功能,别人都有,我怎么能没有? 现在这功能已经不稀奇了,很多手机都带有这个功能. 实现起来不难,用一个蒙版遮在界面上面就行. 至于蒙版,可以用Window ...
- Android护眼模式、屏幕亮度调节功能开发【基于Android8.0】
Android护眼模式.屏幕亮度调节功能开发[基于Android8.0] [引言]网上关于写护眼模式,屏幕亮度调节的android的方法很少,找到的都不能很好的适配Android8.0的版本.于是自己 ...
- 手机腾讯视频软件如何开启护眼功能
今天给大家简单介绍一下,在手机腾讯视频中,如何开启护眼功能,具体步骤如下: 1.首先,打开手机苏宁易购app; 手机腾讯视频软件如何开启护眼功能 2.进入软件时先点击[跳过]广告页面,如图 手机腾讯视 ...
- imoo c1语言设置在哪里,不再被“辣眼睛”!imoo C1 护眼功能解析
最近几年,越来越多厂商为手机加上了"护眼模式",甚至连iPhone也跟风了一回.护眼模式的原理是,把能量较强.给眼睛带来较大伤害的蓝光过滤掉,只留下对眼睛无害的红黄绿. 然后imo ...
- android 护眼模式设置参数,节能护眼看这里!玩转Android手机的显示设置
原标题:节能护眼看这里!玩转Android手机的显示设置 对智能手机而言,除了性能以外,其屏幕的显示效果和续航时间对体验的影响往往更加直观.在系统设置的"显示"功能列表中,我们就能 ...
- android护眼提醒,【Android 应用】护眼提醒.pdf
目录 前言 androidSharedPreference的简单使用 (登陆界面记住密码 ) android通知Notification的使用小实例 (振动 ,灯光 ,声音 ) android调用摄像 ...
- android护眼程序原理,Android 护眼模式的实现
思路:获取content根布局,在上面添加一层浮层,默认透明,开启护眼模式设置护眼色值. 实现:在BaseActivity的onCreate方法中,添加浮层,所有的Activity继承BaseActi ...
- Flutter实现Android护眼模式
有一个奇葩的需求,甲方需要实现Android端护眼需求.查找了一番,发现护眼模式是某些厂商的,某些机型有提供.而且没有对外提供Api调用.于是思索一番,决定自己通过插件方式,使用原生方法实现. 先上效 ...
- Android护眼模式(argb)
前提:护眼模式可以说是加一层某颜色且透明度小于1的view,现在网络上比较流行的护眼色博主比较喜欢以下两种. rgb值如下 第一种:r=199, g=237, b=204: 第一种:r=129, g= ...
最新文章
- android 之 Intent、broadcast
- 交流经过整流桥后的电压计算方法(不权威)
- api服务器开发语言,【API编写】介绍一个国内强大的API接口文档写作网站showdoc - 最好的编程语言 - 博客园...
- 图像拼接c语言,安卓上实现图像拼接(JNI调用NATIVE方法)
- React开发(216):ant dedign 弹窗销毁后再打开,原来的值仍存在,如何销毁弹窗内容?
- Eclipse中SVN过滤指定文件夹或文件下内容
- java查询和添加客户信息_4.从零点五开始的Java之路(增删改查-客户)
- w3school入门自学免费网站推荐
- Wings-让单元测试智能全自动生成
- 简单了解Tomcat与OSGi的类加载器架构
- SQL 比较时间大小
- read H264 Nal
- 阿西莫夫机器人三定律或已过时?伯克利教授定义AI发展“新三原则”!
- 数学分析(2): 数列极限
- 【转载】Web前端框架图
- WIN10 U盘打开无权限问题
- Java手机号码工具类(判断运营商、获取归属地)
- centos 7.x 安装python 3.6 并创建虚拟环境
- 申宝策略-船舶军工表现靓丽
- 简历——“三无”应届生怎么写简历,全是干货!(模板直接拿走)