> tvie.intent.action.REMIND 11-2..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用AlarmManager提醒節(jié)目即將播放

發(fā)布時間:2020-07-05 16:35:33 來源:網(wǎng)絡(luò) 閱讀:381 作者:truesea 欄目:開發(fā)技術(shù)

先看兩段日志:

(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 >> 《藍(lán)籌進(jìn)行時》還有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 >> 《藍(lán)籌進(jìn)行時》還有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分鐘打印出來的,日志顯示程序邏輯沒有說明問題。

以下是程序的實現(xiàn)

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, "節(jié)目提醒", "《" + 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);
    }
}

在以上程序中執(zhí)行了語句:

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

這條語句表示啟動一個鬧鐘服務(wù),每隔60秒執(zhí)行一次操作。這個鬧鐘服務(wù)與具體應(yīng)用無關(guān)。即使應(yīng)用啟動后又完全退出,這個鬧鐘服務(wù)還是運(yùn)行的。

問題:如果下次應(yīng)用再啟動,還會不會再啟動一個鬧鐘服務(wù)呢?如何判斷鬧鐘服務(wù)是否已經(jīng)運(yùn)行,如何避免多次啟動相同的鬧鐘服務(wù)呢?


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

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


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI