<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                > 筆者最近將工具書上Service的有關內容都學習了一下,于是打算做一個小應用來練一下手了。 > 考慮到自己每次在敲代碼或者打游戲的時候總是會不注意時間,一不留神就對著電腦連續3個小時以上,對眼睛的傷害還是挺大的,重度近視了可是會遺傳給將來的孩子的呀,可能老婆都跟別人跑了。 > 于是,為了保護眼睛,筆者便做了個如下的應用: > (界面為了便于讓新手理解,所以做的比較簡單,并且沒有設置背景圖片,也沒有設置APP桌面圖片,有心的讀者完全可以放上自己或者對象的圖片,然后做的比較個人化) **打開后效果:** ![](https://box.kancloud.cn/2016-03-01_56d551ee6d5dc.jpg) **時間到之后有后臺提醒:** ![](https://box.kancloud.cn/2016-03-01_56d551eeb8e67.jpg) ![](https://box.kancloud.cn/2016-03-01_56d551ef420e9.jpg) > 好了,接下來說一下做這樣一個APP主要涉及到的知識點: > **Service:**使用service,便可以在程序即使后臺運行的時候,也能夠做出相應的提醒,并且不影響手機進行其他工作。 > **AlarmManager:**此知識點主要是用來計時,具體的在代碼的注釋中寫的很詳細。 > **notification:**此知識點就是用作通知的顯示了,具體的可以參考筆者的另一篇博客:[http://blog.csdn.net/double2hao/article/details/49421287](http://blog.csdn.net/double2hao/article/details/49421287)[](http://blog.csdn.net/double2hao/article/details/49421287) 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來提醒使用者提醒的功能已經開始 Toast.makeText(MainActivity.this,"提醒的功能已經開啟,關閉界面則會取消提醒。",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分鐘的毫秒數 int Minutes = 90*60*1000; //SystemClock.elapsedRealtime()表示1970年1月1日0點至今所經歷的時間 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表示讓定時任務的出發時間從系統開機算起,并且會喚醒CPU。 manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); //在Service結束后關閉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) { //設置通知內容并在onReceive()這個函數執行時開啟 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="系統默認音樂" 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> ~~~ > > 為了各位讀者方便,在此筆者也是上傳了源碼: > [http://download.csdn.net/detail/double2hao/9252449](http://download.csdn.net/detail/double2hao/9252449)[](http://download.csdn.net/detail/double2hao/9252449) > **此處有個不得不提的注意點**,筆者原來的代碼是在Activity開啟的時候自動開啟Service,在Activity摧毀的時候自動摧毀Service,看上去好像可以運行,沒有什么錯誤,并且在**10分鐘內**的提醒基本都能夠正常運行。 > 但是倘若在比較長的時間提醒的時候就會出現**不提醒**的問題了!為什么呢,因為**android為了優化內存,減少耗電**,是會**自動清理內存**的,**會把后臺的Service給清理掉。** > 至于具體怎么優化,還是暫且給讀者們一個自我學習的空間吧。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看