iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android后台定时提醒功能实现
  • 717
分享到

Android后台定时提醒功能实现

Android 2022-06-06 09:06:05 717人浏览 八月长安
摘要

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

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

时间到之后有后台提醒:

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

Service:使用service,便可以在程序即使后台运行的时候,也能够做出相应的提醒,并且不影响手机进行其他工作。
AlarmManager:此知识点主要是用来计时,具体的在代码的注释中写的很详细。
notification:此知识点就是用作通知的显示了,具体的可以参考另一篇文章:

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; 
 @Override 
 protected 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); 
  //开启关闭Service 
  startService(intent); 
  //设置一个Toast来提醒使用者提醒的功能已经开始 
  Toast.makeText(MainActivity.this,"提醒的功能已经开启,关闭界面则会取消提醒。",Toast.LENGTH_LONG).show(); 
 } 
 @Override 
 protected void onDestroy() { 
  super.onDestroy(); 
  //在Activity被关闭后,关闭Service 
  stopService(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 { 
 @Override 
 public IBinder onBind(Intent intent) { 
  return null; 
 } 
 @Override 
 public 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这个Service 
  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); 
 } 
} 

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 { 
 @Override 
 public 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> 

此处有个不得不提的注意点,笔者原来的代码是在Activity开启的时候自动开启Service,在Activity摧毁的时候自动摧毁Service,看上去好像可以运行,没有什么错误,并且在10分钟内的提醒基本都能够正常运行。
但是倘若在比较长的时间提醒的时候就会出现不提醒的问题了!为什么呢?

因为android为了优化内存,减少耗电,是会自动清理内存的,会把后台的Service给清理掉。

您可能感兴趣的文章:Android4.4开发之电池低电量告警提示原理与实现方法分析Android基于广播事件机制实现简单定时提醒功能代码android获取情景模式和铃声 实现震动、铃声提醒详解Android中Notification通知提醒Android编程设置提醒事件的方法Android高仿微信5.2.1主界面及消息提醒Android实现每天定时提醒功能Android提醒微技巧你真的了解Dialog、Toast和Snackbar吗Android 开发之Dialog,Toast,Snackbar提醒Android开发之使用通知栏显示提醒信息的方法Android编程实现添加低电流提醒功能的方法


--结束END--

本文标题: Android后台定时提醒功能实现

本文链接: https://www.lsjlt.com/news/25938.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作