温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用AlarmManager提醒节目即将播放

发布时间:2020-07-05 16:35:33 来源:网络 阅读:372 作者: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