Android后臺定時提醒功能實現(xiàn)
前提:考慮到自己每次在敲代碼或者打游戲的時候總是會不注意時間,一不留神就對著電腦連續(xù)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來提醒使用者提醒的功能已經(jīng)開始 Toast.makeText(MainActivity.this,"提醒的功能已經(jīng)開啟,關閉界面則會取消提醒。",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分鐘的毫秒數(shù) int Minutes = 90*60*1000; //SystemClock.elapsedRealtime()表示1970年1月1日0點至今所經(jīng)歷的時間 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表示讓定時任務的出發(fā)時間從系統(tǒng)開機算起,并且會喚醒CPU。 manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); //在Service結(jié)束后關閉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) { //設置通知內(nèi)容并在onReceive()這個函數(shù)執(zhí)行時開啟 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="系統(tǒng)默認音樂" 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分鐘內(nèi)的提醒基本都能夠正常運行。
但是倘若在比較長的時間提醒的時候就會出現(xiàn)不提醒的問題了!為什么呢?
因為android為了優(yōu)化內(nèi)存,減少耗電,是會自動清理內(nèi)存的,會把后臺的Service給清理掉。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。
- Android4.4開發(fā)之電池低電量告警提示原理與實現(xiàn)方法分析
- Android基于廣播事件機制實現(xiàn)簡單定時提醒功能代碼
- android獲取情景模式和鈴聲 實現(xiàn)震動、鈴聲提醒
- 詳解Android中Notification通知提醒
- Android編程設置提醒事件的方法
- Android高仿微信5.2.1主界面及消息提醒
- Android實現(xiàn)每天定時提醒功能
- Android提醒微技巧你真的了解Dialog、Toast和Snackbar嗎
- Android 開發(fā)之Dialog,Toast,Snackbar提醒
- Android開發(fā)之使用通知欄顯示提醒信息的方法
- Android編程實現(xiàn)添加低電流提醒功能的方法
相關文章
Android 使用URLConnection下載音頻文件的方法
有時候我們會需要下載音頻文件。這里提供一種思路,將在線音頻文件通過流寫到本地文件中。需要的朋友可以參考下2019-09-09Android多功能視頻播放器GSYVideoPlayer開發(fā)流程
怎么在Android中實現(xiàn)GSYVideoPlayer視頻播放器?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲2022-11-11Android RecyclerView藝術般的控件使用完全解析
這篇文章主要介紹了Android RecyclerView藝術般的控件使用完全解析的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07Android仿微信朋友圈實現(xiàn)滾動條下拉反彈效果
這篇文章主要為大家介紹了Android仿微信朋友圈實現(xiàn)滾動條下拉反彈效果,感興趣的小伙伴們可以參考一下2016-01-01