android 后台定时提醒(Service,AlarmManager的使用)

来源:互联网 发布:手机游戏直播软件 编辑:程序博客网 时间:2024/06/13 04:36

笔者最近将工具书上Service的有关内容都学习了一下,于是打算做一个小应用来练一下手了。

考虑到自己每次在敲代码或者打游戏的时候总是会不注意时间,一不留神就对着电脑连续3个小时以上,对眼睛的伤害还是挺大的,重度近视了可是会遗传给将来的孩子的呀,可能老婆都跟别人跑了。

于是,为了保护眼睛,笔者便做了个如下的应用:

(界面为了便于让新手理解,所以做的比较简单,并且没有设置背景图片,也没有设置APP桌面图片,有心的读者完全可以放上自己或者对象的图片,然后做的比较个人化)


打开后效果:





时间到之后有后台提醒:







好了,接下来说一下做这样一个APP主要涉及到的知识点:


Service:使用service,便可以在程序即使后台运行的时候,也能够做出相应的提醒,并且不影响手机进行其他工作。

AlarmManager:此知识点主要是用来计时,具体的在代码的注释中写的很详细。

notification:此知识点就是用作通知的显示了,具体的可以参考笔者的另一篇博客:http://blog.csdn.net/double2hao/article/details/49421287




MainActivity:

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Window;import android.widget.Toast;public class MainActivity extends Activity {private Intent intent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//取消标题栏requestWindowFeature(Window.FEATURE_NO_TITLE);//由于主要是用于测试,并且便于新手理解,所以activity_main布局写的很简单setContentView(R.layout.activity_main);intent = new Intent(this, LongRunningService.class);//开启关闭ServicestartService(intent);//设置一个Toast来提醒使用者提醒的功能已经开始Toast.makeText(MainActivity.this,"提醒的功能已经开启,关闭界面则会取消提醒。",Toast.LENGTH_LONG).show();}@Overrideprotected void onDestroy() {super.onDestroy();//在Activity被关闭后,关闭ServicestopService(intent);}}


LongRunningService:

import android.app.AlarmManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.SystemClock;public class LongRunningService extends Service {@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);//读者可以修改此处的Minutes从而改变提醒间隔时间//此处是设置每隔90分钟启动一次//这是90分钟的毫秒数int Minutes = 90*60*1000;//SystemClock.elapsedRealtime()表示1970年1月1日0点至今所经历的时间long triggerAtTime = SystemClock.elapsedRealtime() + Minutes;//此处设置开启AlarmReceiver这个ServiceIntent 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);}}


AlarmReceiver:

import android.app.Notification;import android.app.NotificationManager;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class AlarmReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {//设置通知内容并在onReceive()这个函数执行时开启NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);Notification notification=new Notification(R.drawable.ic_launcher,"用电脑时间过长了!白痴!",System.currentTimeMillis());notification.setLatestEventInfo(context, "快去休息!!!","一定保护眼睛,不然遗传给孩子,老婆跟别人跑啊。", null);notification.defaults = Notification.DEFAULT_ALL;manager.notify(1, notification);//再次开启LongRunningService这个服务,从而可以Intent i = new Intent(context, LongRunningService.class);context.startService(i);}}


activity_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="15dp"    android:orientation="vertical"    >    <TextView        android:layout_marginBottom="20dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="护眼定时提醒"        android:textSize="30sp"        android:gravity="center_horizontal"        />        <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="提醒间隔时间:"        android:textSize="25sp"        />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="90分钟"        android:textSize="25sp"        />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="提醒音乐:"        android:textSize="25sp"        />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="系统默认音乐"        android:textSize="25sp"        /></LinearLayout>


千万不要忘了在AndroidManifest中注册Service!

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.servicebestpractice"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.servicebestpractice.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service android:name=".LongRunningService" >        </service>        <receiver android:name=".AlarmReceiver" >        </receiver>    </application></manifest>


为了各位读者方便,在此笔者也是上传了源码:

http://download.csdn.net/detail/double2hao/9252449



此处有个不得不提的注意点,笔者原来的代码是在Activity开启的时候自动开启Service,在Activity摧毁的时候自动摧毁Service,看上去好像可以运行,没有什么错误,并且在10分钟内的提醒基本都能够正常运行。

但是倘若在比较长的时间提醒的时候就会出现不提醒的问题了!为什么呢,因为android为了优化内存,减少耗电,是会自动清理内存的,会把后台的Service给清理掉。

至于具体怎么优化,还是暂且给读者们一个自我学习的空间吧。

3 1
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 11个月的宝宝大便干燥怎么办 1岁宝宝又拉又吐怎么办 怀孕八个月了不想要了怎么办 奶水不够宝宝又不喝奶粉怎么办 手机恢复出厂设置密码忘了怎么办 5s锁屏密码忘了怎么办 深圳房子卖了户口没地方迁怎么办 宝马1系130i烧机怎么办 小孩流清鼻涕怎么办最简单方法 孕3个月胎盘低置怎么办 孩子判逆不听家长的话该怎么办 香港购物超5000被海关扣怎么办 浅色衣服被深色衣服染色了怎么办 金立手机微信不能发语音怎么办 吃鸡买的账号密码邮箱忘记了怎么办 氩弧焊枪管带里进水了怎么办 绝地求生穿头盔的时候连衣帽怎么办 开车不小心把光缆线给挂断了怎么办 脚刺到了生锈钢钉没打针怎么办 一加3t背壳螺丝掉了怎么办 30万美金美金中国被扣怎么办 电脑使用迅雷变的很卡怎么办 优盘拷贝过程中失去优盘路径怎么办 用百度云上传视频文件太慢了怎么办 网易云音乐云盘电脑上传很慢怎么办 路由器的宽带账号密码忘记了怎么办 蚂蚁邦路由器管理密码忘记了怎么办 红米2a刷机失败怎么办 小米手机开机图案锁忘记了怎么办 小米6进水无限闪屏开机重启怎么办 红米手机一直卡在开机画面怎么办 红米4卡在开机画面怎么办 红米手机一直在开机画面怎么办 红米手机一直跳开机画面怎么办 红米note3锁屏密码忘记怎么办 红米手机忘记锁屏密码怎么办 红米4锁屏密码忘了怎么办 红米note忘记锁屏密码怎么办 红米note2锁屏密码忘了怎么办 机打发票抬头名字少写一个字怎么办 卷式发票名字写错了怎么办