天天看點

VLC-android開機啟動部分

比較值得推薦的開機啟動方式。

收到開機廣播後,設定一個循環的間隔5s執行定時器,來一直啟動一個service。這樣做可以避免程式起不來,也可以避免程式因為出現異常沒有起來了。

使用的定時器也是用一個比較節省的方式,

/*****************************************************************************
 * BootupReceiver.java
 *****************************************************************************
 * Copyright © 2014-2015 VLC authors, VideoLAN and VideoLabs
 * Author: Geoffrey Métais
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
package org.videolan.vlc;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import org.videolan.vlc.util.AndroidDevices;

public class BootupReceiver extends BroadcastReceiver {
    public BootupReceiver() {
    }
    private static final String TAG = "VLC/BootupReceiver";

    private static final long INITIAL_DELAY = 5000;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (AndroidDevices.isAndroidTv() && intent.getAction().endsWith(Intent.ACTION_BOOT_COMPLETED)) {
            Log.d(TAG, "ACTION_BOOT_COMPLETED ");
            scheduleRecommendationUpdate(context);
        }
    }

    private void scheduleRecommendationUpdate(Context context) {
        AlarmManager alarmManager = (AlarmManager) VLCApplication.getAppContext().getSystemService(
                Context.ALARM_SERVICE);
        Intent recommendationIntent = new Intent(context,
                RecommendationsService.class);
        PendingIntent alarmIntent = PendingIntent.getService(context, 0,
                recommendationIntent, 0);

        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                INITIAL_DELAY,
                AlarmManager.INTERVAL_HOUR,
                alarmIntent);
    }
}      

相關知識:

摘自http://www.open-open.com/lib/view/open1350291466977.html

AlarmManager的作用文檔中的解釋是:在特定的時刻為我們廣播一個指定的Intent。簡單的說就是我們設定一個時間,然後在該時間到來時,AlarmManager為我們廣播一個我們設定的Intent,常用方法有五個:

(1)set(int type,long startTime,PendingIntent pi);

        該方法用于設定一次性鬧鐘,第一個參數表示鬧鐘類型,第二個參數表示鬧鐘執行時間,第三個參數表示鬧鐘響應動作。

(2)setRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

        該方法用于設定重複鬧鐘,第一個參數表示鬧鐘類型,第二個參數表示鬧鐘首次執行時間,第三個參數表示鬧鐘兩次執行的間隔時間,第四個參數表示鬧鐘響應動作。類似JAVA的Timer裡面

scheduleAtFixedRate(TimerTask task, long delay, long period):

以近似固定的時間間隔(由指定的周期分隔)進行後續執行。在固定速率執行中,根據已安排的初始執行時間來安排每次執行。如果由于任何原因(如垃圾回收或其他背景活動)而延遲了某次執行,則将快速連續地出現兩次或更多的執行,進而使後續執行能夠“追趕上來”。從長遠來看,執行的頻率将正好是指定周期的倒數(假定 Object.wait(long) 所依靠的系統時鐘是準确的)。

setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

        該方法也用于設定重複鬧鐘,與第二個方法相似,不過其兩個鬧鐘執行的間隔時間不是固定的而已。它相對而言更節能(power-efficient)一些,因為系統可能會将幾個差不多的鬧鐘合并為一個來執行,減少裝置的喚醒次數。 有點類似JAVA的Timer裡面schedule(TimerTask task, Date firstTime, long period):根據前一次執行的實際執行時間來安排每次執行。如果由于任何原因(如垃圾回收或其他背景活動)而延遲了某次執行,則後續執行也将被延遲。在長期運作中,執行的頻率一般要稍慢于指定周期的倒數(假定 Object.wait(long) 所依靠的系統時鐘是準确的)。

cancel(PendingIntent operation) 

setTimeZone(String timeZone)