最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年01月08日 14:32:18   作者:xu佳佳  
這篇文章主要介紹了Android后臺(tái)定時(shí)提醒功能,針對(duì)Service,AlarmManager的使用進(jìn)行詳細(xì)闡述,感興趣的小伙伴們可以參考一下

前提:考慮到自己每次在敲代碼或者打游戲的時(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í)有所幫助。

相關(guān)文章

  • ImageView點(diǎn)擊可變暗的實(shí)例代碼(android代碼技巧)

    ImageView點(diǎn)擊可變暗的實(shí)例代碼(android代碼技巧)

    本文給大家分享一段實(shí)例代碼給大家介紹ImageView點(diǎn)擊可變暗的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧
    2017-02-02
  • Android基于OpenCV實(shí)現(xiàn)霍夫直線檢測

    Android基于OpenCV實(shí)現(xiàn)霍夫直線檢測

    霍夫變換利用點(diǎn)與線之間的對(duì)偶性,將圖像空間中直線上離散的像素點(diǎn)通過參數(shù)方程映射為霍夫空間中的曲線,并將霍夫空間中多條曲線的交點(diǎn)作為直線方程的參數(shù)映射為圖像空間中的直線。給定直線的參數(shù)方程,可以利用霍夫變換來檢測圖像中的直線。本文簡單講解Android的實(shí)現(xiàn)
    2021-06-06
  • Android Studio生成 Flutter 模板代碼技巧詳解

    Android Studio生成 Flutter 模板代碼技巧詳解

    這篇文章主要為大家介紹了Android Studio生成 Flutter 模板代碼技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 教你輕松制作Android音樂播放器

    教你輕松制作Android音樂播放器

    這篇文章主要教大家輕松制作Android音樂播放器,制作一款屬于自己的Android音樂播放器,希望大家喜歡。
    2015-11-11
  • Android使用自定義PageTransformer實(shí)現(xiàn)個(gè)性的ViewPager動(dòng)畫切換效果

    Android使用自定義PageTransformer實(shí)現(xiàn)個(gè)性的ViewPager動(dòng)畫切換效果

    這篇文章主要介紹了Android使用自定義PageTransformer實(shí)現(xiàn)個(gè)性的ViewPager切換動(dòng)畫,具有很好的參考價(jià)值,一起跟隨小編過來看看吧
    2018-05-05
  • android重力感應(yīng)開發(fā)之微信搖一搖功能

    android重力感應(yīng)開發(fā)之微信搖一搖功能

    這篇文章主要為大家詳細(xì)介紹了android重力感應(yīng)開發(fā)之微信搖一搖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android Studio中生成aar文件及本地方式使用aar文件的方法

    Android Studio中生成aar文件及本地方式使用aar文件的方法

    這篇文章給大家講解Android Studio中生成aar文件以及本地方式使用aar文件的方法,也就是說 *.jar 與 *.aar 的生成與*.aar導(dǎo)入項(xiàng)目方法,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2017-12-12
  • 解析android 流量監(jiān)測的實(shí)現(xiàn)原理

    解析android 流量監(jiān)測的實(shí)現(xiàn)原理

    本篇文章是對(duì)android中流量監(jiān)測的實(shí)現(xiàn)原理進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Android內(nèi)存泄漏導(dǎo)致原因深入探究

    Android內(nèi)存泄漏導(dǎo)致原因深入探究

    內(nèi)存管理的目的就是讓我們?cè)陂_發(fā)過程中有效避免我們的應(yīng)用程序出現(xiàn)內(nèi)存泄露的問題。內(nèi)存泄露相信大家都不陌生,我們可以這樣理解:沒有用的對(duì)象無法回收的現(xiàn)象就是內(nèi)存泄露
    2023-02-02
  • Android+OpenCV4.2.0環(huán)境配置詳解(Android studio)

    Android+OpenCV4.2.0環(huán)境配置詳解(Android studio)

    這篇文章主要介紹了Android+OpenCV4.2.0環(huán)境配置詳解(Android studio),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10

最新評(píng)論

石楼县| 清新县| 株洲县| 佛学| 乌兰察布市| 桑植县| 陇川县| 永登县| 周至县| 洮南市| 上饶县| 湘潭市| 通城县| 宜春市| 富民县| 江源县| 南宁市| 鄂托克前旗| 麟游县| 玉树县| 施甸县| 眉山市| 靖边县| 云南省| 长泰县| 宁津县| 神池县| 施甸县| 砀山县| 喜德县| 东兴市| 佛山市| 富裕县| 宁明县| 门头沟区| 临澧县| 来宾市| 西华县| 珲春市| 南岸区| 奉贤区|