国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android后臺(tái)定時(shí)提醒功能實(shí)現(xiàn)

2020-04-11 11:02:41
字體:
供稿:網(wǎng)友

前提:考慮到自己每次在敲代碼或者打游戲的時(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);   //由于主要是用于測試,并且便于新手理解,所以activity_main布局寫的很簡單   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í)間過長了!白癡!"   ,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)行。
但是倘若在比較長的時(shí)間提醒的時(shí)候就會(huì)出現(xiàn)不提醒的問題了!為什么呢?

因?yàn)閍ndroid為了優(yōu)化內(nèi)存,減少耗電,是會(huì)自動(dòng)清理內(nèi)存的,會(huì)把后臺(tái)的Service給清理掉。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 昂仁县| 普兰县| 延边| 象州县| 兴化市| 洛浦县| 沙田区| 阿合奇县| 柳州市| 咸宁市| 井冈山市| 虹口区| 祁连县| 抚远县| 祁门县| 栾川县| 来宾市| 雅江县| 阿巴嘎旗| 固始县| 奉节县| 通河县| 德格县| 涟水县| 吉林市| 扎鲁特旗| 漳州市| 延吉市| 威远县| 石家庄市| 临清市| 荃湾区| 华池县| 公安县| 广丰县| 蕉岭县| 荔波县| 临海市| 固镇县| 中卫市| 枣强县|