Android后臺(tái)定時(shí)提醒功能實(shí)現(xiàn)
前提:考慮到自己每次在敲代碼或者打游戲的時(shí)候總是會(huì)不注意時(shí)間,一不留神就對(duì)著電腦連續(xù)3個(gè)小時(shí)以上,對(duì)眼睛的傷害還是挺大的,重度近視了可是會(huì)遺傳給將來的孩子的呀,可能老婆都跟別人跑了。
于是,為了保護(hù)眼睛,便做了個(gè)如下的應(yīng)用:
打開后效果:
時(shí)間到之后有后臺(tái)提醒:
好了,接下來說一下做這樣一個(gè)APP主要涉及到的知識(shí)點(diǎn):
Service:使用service,便可以在程序即使后臺(tái)運(yùn)行的時(shí)候,也能夠做出相應(yīng)的提醒,并且不影響手機(jī)進(jìn)行其他工作。
AlarmManager:此知識(shí)點(diǎn)主要是用來計(jì)時(shí),具體的在代碼的注釋中寫的很詳細(xì)。
notification:此知識(shí)點(diǎn)就是用作通知的顯示了,具體的可以參考另一篇文章:
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); //取消標(biāo)題欄 requestWindowFeature(Window.FEATURE_NO_TITLE); //由于主要是用于測(cè)試,并且便于新手理解,所以activity_main布局寫的很簡(jiǎn)單 setContentView(R.layout.activity_main); intent = new Intent(this, LongRunningService.class); //開啟關(guān)閉Service startService(intent); //設(shè)置一個(gè)Toast來提醒使用者提醒的功能已經(jīng)開始 Toast.makeText(MainActivity.this,"提醒的功能已經(jīng)開啟,關(guān)閉界面則會(huì)取消提醒。",Toast.LENGTH_LONG).show(); } @Override protected void onDestroy() { super.onDestroy(); //在Activity被關(guān)閉后,關(guān)閉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從而改變提醒間隔時(shí)間 //此處是設(shè)置每隔90分鐘啟動(dòng)一次 //這是90分鐘的毫秒數(shù) int Minutes = 90*60*1000; //SystemClock.elapsedRealtime()表示1970年1月1日0點(diǎn)至今所經(jīng)歷的時(shí)間 long triggerAtTime = SystemClock.elapsedRealtime() + Minutes; //此處設(shè)置開啟AlarmReceiver這個(gè)Service Intent i = new Intent(this, AlarmReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0); //ELAPSED_REALTIME_WAKEUP表示讓定時(shí)任務(wù)的出發(fā)時(shí)間從系統(tǒng)開機(jī)算起,并且會(huì)喚醒CPU。 manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); //在Service結(jié)束后關(guān)閉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) { //設(shè)置通知內(nèi)容并在onReceive()這個(gè)函數(shù)執(zhí)行時(shí)開啟 NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification=new Notification(R.drawable.ic_launcher,"用電腦時(shí)間過長(zhǎng)了!白癡!" ,System.currentTimeMillis()); notification.setLatestEventInfo(context, "快去休息?。?!", "一定保護(hù)眼睛,不然遺傳給孩子,老婆跟別人跑啊。", null); notification.defaults = Notification.DEFAULT_ALL; manager.notify(1, notification); //再次開啟LongRunningService這個(gè)服務(wù),從而可以 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="護(hù)眼定時(shí)提醒" android:textSize="30sp" android:gravity="center_horizontal" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提醒間隔時(shí)間:" 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="系統(tǒng)默認(rèn)音樂" android:textSize="25sp" /> </LinearLayout>
千萬不要忘了在AndroidManifest中注冊(cè)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>
此處有個(gè)不得不提的注意點(diǎn),筆者原來的代碼是在Activity開啟的時(shí)候自動(dòng)開啟Service,在Activity摧毀的時(shí)候自動(dòng)摧毀Service,看上去好像可以運(yùn)行,沒有什么錯(cuò)誤,并且在10分鐘內(nèi)的提醒基本都能夠正常運(yùn)行。
但是倘若在比較長(zhǎng)的時(shí)間提醒的時(shí)候就會(huì)出現(xiàn)不提醒的問題了!為什么呢?
因?yàn)閍ndroid為了優(yōu)化內(nèi)存,減少耗電,是會(huì)自動(dòng)清理內(nèi)存的,會(huì)把后臺(tái)的Service給清理掉。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android4.4開發(fā)之電池低電量告警提示原理與實(shí)現(xiàn)方法分析
- Android基于廣播事件機(jī)制實(shí)現(xiàn)簡(jiǎn)單定時(shí)提醒功能代碼
- android獲取情景模式和鈴聲 實(shí)現(xiàn)震動(dòng)、鈴聲提醒
- 詳解Android中Notification通知提醒
- Android編程設(shè)置提醒事件的方法
- Android高仿微信5.2.1主界面及消息提醒
- Android實(shí)現(xiàn)每天定時(shí)提醒功能
- Android提醒微技巧你真的了解Dialog、Toast和Snackbar嗎
- Android 開發(fā)之Dialog,Toast,Snackbar提醒
- Android開發(fā)之使用通知欄顯示提醒信息的方法
- Android編程實(shí)現(xiàn)添加低電流提醒功能的方法
相關(guān)文章
Android 使用URLConnection下載音頻文件的方法
有時(shí)候我們會(huì)需要下載音頻文件。這里提供一種思路,將在線音頻文件通過流寫到本地文件中。需要的朋友可以參考下2019-09-09Android多功能視頻播放器GSYVideoPlayer開發(fā)流程
怎么在Android中實(shí)現(xiàn)GSYVideoPlayer視頻播放器?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲2022-11-11Android 谷歌推薦的VR實(shí)現(xiàn)方式(分享)
下面小編就為大家分享一篇Android 谷歌推薦的VR實(shí)現(xiàn)方式。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android RecyclerView藝術(shù)般的控件使用完全解析
這篇文章主要介紹了Android RecyclerView藝術(shù)般的控件使用完全解析的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android adb.exe程序啟動(dòng)不起來 具體解決方法
這篇文章主要介紹了Android adb.exe程序啟動(dòng)不起來 具體解決方法,有需要的朋友可以參考一下2013-12-12Android仿微信朋友圈實(shí)現(xiàn)滾動(dòng)條下拉反彈效果
這篇文章主要為大家介紹了Android仿微信朋友圈實(shí)現(xiàn)滾動(dòng)條下拉反彈效果,感興趣的小伙伴們可以參考一下2016-01-01