android提醒功能,android service实现循环定时提醒功能

本文介绍了如何使用Android Service实现一个周期性的喝水提醒功能,即使应用被关闭也能继续工作。关键在于使用AlarmManager进行定时任务设置,并通过BroadcastReceiver在触发时重新启动Service。此外,代码展示了如何创建AlertDialog和播放闹铃。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

人每天都要喝8杯水才能保持健康,于是苦逼的程序员总是一遍代码就忘了时间,于是我突发奇想能不能开发一个apk能够实现固定的间隔时间定时提醒我要喝水了呢?

apk基本功能:

1)能够设置间隔时间 2)在apk应用被停止的情况下仍然能定时提醒 3)能够播放指定闹铃 4)能够及时终止提醒

效果图:

设置间隔

9c9bd750f662391193a14a63cf80cb91.png

时间到后会跳出全局AlertDialog提示并且开始播放闹铃

52ed1dbf2494fe906bde6b91f9f90b26.png

即使APP被终止了,仍然能够提示

9816d18cb248f5d87faf5259454e2bdc.png

结束提示

29b67752b1fc603b5faf5ad1fd2bed7b.png

废话不多说,直接上代码:

布局layout:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="bai.cslg.servicebestpractice.MainActivity"

android:baselineAligned="false"

android:orientation="vertical">

android:paddingTop="20dp"

android:layout_width="match_parent"

android:layout_height="70dp">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="5"

android:padding="10dp"

android:gravity="center_vertical"

android:text="请设置提示时间间隔:"

android:textSize="20sp"/>

android:id="@+id/time"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:gravity="center_vertic

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值