使用AlarmManager提醒節目即將播放

先看兩段日誌:

(1)日誌打印的是18點19分18秒的信息。

11-21 18:19:18.587: V/TvRemindReceiver(29899): @onReceive. action >> tvie.intent.action.REMIND
11-21 18:19:18.597: V/TvRemindReceiver(29899): @onRemind. cursor.count >> 3
11-21 18:19:18.647: V/TvRemindReceiver(29899): @remind. notify user to watch TV. notificationId is >> 4
11-21 18:19:18.647: V/TvRemindReceiver(29899): @remind. notify user to watch TV. message is >> 《藍籌進行時》還有5分鐘就開始播放。
11-21 18:19:18.707: V/TvRemindReceiver(29899): @remind. notify user to watch TV. notificationId is >> 5
11-21 18:19:18.707: V/TvRemindReceiver(29899): @remind. notify user to watch TV. message is >> 《理財晚餐》還有5分鐘就開始播放。
11-21 18:19:18.717: V/TvRemindReceiver(29899): @remind. notify user to watch TV. notificationId is >> 6
11-21 18:19:18.717: V/TvRemindReceiver(29899): @remind. notify user to watch TV. message is >> 《中國基金報道》還有5分鐘就開始播放。

(2)日誌打印的是18點20分18秒的信息。

11-21 18:20:18.587: V/TvRemindReceiver(29899): @onReceive. action >> tvie.intent.action.REMIND
11-21 18:20:18.597: V/TvRemindReceiver(29899): @onRemind. cursor.count >> 3
11-21 18:20:18.627: V/TvRemindReceiver(29899): @remind. notify user to watch TV. notificationId is >> 7
11-21 18:20:18.627: V/TvRemindReceiver(29899): @remind. notify user to watch TV. message is >> 《藍籌進行時》還有5分鐘就開始播放。
11-21 18:20:18.677: V/TvRemindReceiver(29899): @remind. notify user to watch TV. notificationId is >> 8
11-21 18:20:18.677: V/TvRemindReceiver(29899): @remind. notify user to watch TV. message is >> 《理財晚餐》還有5分鐘就開始播放。
11-21 18:20:18.677: V/TvRemindReceiver(29899): @remind. notify user to watch TV. notificationId is >> 9
11-21 18:20:18.677: V/TvRemindReceiver(29899): @remind. notify user to watch TV. message is >> 《中國基金報道》還有5分鐘就開始播放。

顯然以上兩段日誌是相隔1分鐘打印出來的,日誌顯示程序邏輯沒有說明問題。

以下是程序的實現

public class TvRemindReceiver extends BroadcastReceiver {
    public static final String ACTION_REMIND = "tvie.intent.action.REMIND";
    public static final String ACTION_LAUNCH = "tvie.intent.action.LAUNCH";
    public static final int TYPE_REMIND = 1;
    public static final int TYPE_LAUNCH = 2;
    private String TAG = "TvRemindReceiver";
    private static int notificationId = 0;
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Logger.v(TAG, "@onReceive. action >> " + action);
        if(action == null) return;
        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            onBootCompleted(context);
        } else if (action.equals(ACTION_LAUNCH)) {
            onBootCompleted(context);
        } else if (action.equals(ACTION_REMIND)) {
            onRemind(context);
        }
    }
    private void onBootCompleted(Context context) {
        Logger.v(TAG, "@onBootCompleted.");
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, TvRemindReceiver.class);
        intent.setAction(ACTION_REMIND);
        PendingIntent operation = PendingIntent.getBroadcast(context, 0, intent, 0);
        long triggerAtMillis = SystemClock.elapsedRealtime();
        am.setRepeating(TYPE_REMIND, triggerAtMillis, 60 * 1000, operation);
    }
                                                                                                                                                                              
    public int getCursorCount(Cursor cursor) {
                                                                                                                                                                                  
        int count = 0;
        while(cursor.moveToNext()) {
            count++;
        }
        return count;
    }
    private void onRemind(Context context) {
        Cursor cursor = context.getContentResolver().query(DataProvider.getUri(), null,
                SimpleDataColumns.MODULE + "= ? ", new String[] { Constants.PROGRAM }, null);
        TvRemind tvRemind = null;
        Logger.v(TAG, "@onRemind. cursor.count >> " + getCursorCount(cursor));
        if(cursor.moveToFirst()) {
            do{
                String programId = cursor.getString(cursor.getColumnIndex(SimpleDataColumns.KEY));
                String time = cursor.getString(cursor.getColumnIndex(SimpleDataColumns.DATA1));
                String name = cursor.getString(cursor.getColumnIndex(SimpleDataColumns.DATA2));
                tvRemind = new TvRemind(time, name, programId);
                remind(context, R.drawable.ic_launcher, "節目提醒", "《" + tvRemind.getName() + "》還有5分鐘就開始播放。");
            }while(cursor.moveToNext());
        }
        cursor.close();
    }
    @SuppressWarnings("deprecation")
    private void remind(Context context, int icon, String title, String text) {
        Notification notifaction = new Notification();
        notifaction.icon = icon;
        notifaction.tickerText = text;
        notifaction.when = System.currentTimeMillis();
        notifaction.defaults = Notification.DEFAULT_ALL;
        Intent it = new Intent(context, MainActivity.class);
        PendingIntent operation = PendingIntent.getBroadcast(context, notificationId, it,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notifaction.setLatestEventInfo(context, title, text, operation);
                                                                                                                                                                                  
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(++notificationId, notifaction);
        Logger.v(TAG, "@remind. notify user to watch TV. notificationId is >> " + notificationId);
        Logger.v(TAG, "@remind. notify user to watch TV. message is >> " + text);
    }
}

在以上程序中執行了語句:

am.setRepeating(TYPE_REMIND, triggerAtMillis, 60 * 1000, operation);

這條語句表示啓動一個鬧鐘服務,每隔60秒執行一次操作。這個鬧鐘服務與具體應用無關。即使應用啓動後又完全退出,這個鬧鐘服務還是運行的。

問題:如果下次應用再啓動,還會不會再啓動一個鬧鐘服務呢?如何判斷鬧鐘服務是否已經運行,如何避免多次啓動相同的鬧鐘服務呢?


參考:1. AlarmManager(全局定時器/鬧鐘)指定時長或以週期形式執行某項操作

http://www.cnblogs.com/jico/archive/2010/11/03/1868361.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章