91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

使用AlarmManager提醒節目即將播放

發布時間:2020-07-05 16:35:33 來源:網絡 閱讀:381 作者:truesea 欄目:開發技術

先看兩段日志:

(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


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

德安县| 泰州市| 静乐县| 固始县| 翼城县| 衡阳县| 沿河| 田阳县| 凭祥市| 沾益县| 衡南县| 张家港市| 鄂尔多斯市| 梁山县| 东山县| 太谷县| 萨嘎县| 太仆寺旗| 资讯| 大姚县| 循化| 鄂伦春自治旗| 荥阳市| 清涧县| 龙井市| 宿迁市| 临湘市| 广安市| 渭源县| 滦南县| 阿拉善左旗| 罗甸县| 达州市| 华蓥市| 湖北省| 三门峡市| 濉溪县| 长治市| 寿光市| 安仁县| 岢岚县|