Android护眼功能

Android护眼功能,第1张

概述目前全国已有3亿多视光疾病患者,每年眼镜需求量达7000万副,近视率从过去的全球排名第四上升到第二,仅次于日本。保护眼睛成了一个刻不容缓的话题。对于很多学生、上班族来说,除了每天大量时间对着书本、电脑外,恐怕眼睛最大的焦点都是放在手机上,因此,一款能够保护眼睛的手机软件就

目前全国已有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
<application        androID: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 {	@OverrIDe	public 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 {	@OverrIDe	public IBinder onBind(Intent intent) {		return null;	}	@OverrIDe	public 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这个broadcastReceiver		Intent 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);	}	@OverrIDe	public voID onDestroy() {		super.onDestroy();		//在Service结束后关闭AlarmManager		AlarmManager 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:

<linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:orIEntation="vertical"><button                androID: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护眼功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1058709.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存