安卓时间相关的广播(Intent.ACTION_TIME_TICK)

1.Intent.ACTION_TIME_TICK 含义:系统每分钟会发出该广播
Intent.ACTION_TIME_CHANGED); // 时间改变,例如手动修改设置里的时间
Intent.ACTION_TIMEZONE_CHANGED); // 时区变化,例如手动修改设置里的时区

2.用法:

    IntentFilter filter = new IntentFilter(); 
    filter.addAction(Intent.ACTION_TIME_TICK);
    filter.addAction(Intent.ACTION_TIME_CHANGED);
    registerReceiver(broadcastReceiver, filter);
    //广播的注册,其中Intent.ACTION_TIME_CHANGED代表时间设置变化的时候会发出该广播


    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
                if(intent.ACTION_TIME_TICK.equals(intent.getAction())){
     updateTime();//每一分钟更新时间
     }else if(intent.ACTION_TIME_CHANGED.equals(intent.getAction())){

            }
        }
    };

public String updateTime() {
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        boolean is24hFormart = true;
        if (!is24hFormart && hour >= 12) {
            hour = hour - 12;
        }

        String time = "";
        if (hour >= 10) {
            time += Integer.toString(hour);
        }
        else {
            time += "0" + Integer.toString(hour);
        }
        time += ":";

        if (minute >= 10) {
            time += Integer.toString(minute);
        }
        else {
            time += "0" + Integer.toString(minute);
        }

        return time;
    }

3.注意:ACTION_TIME_TICK不能用于静态注册,原因为:

1.提高系统效率:这两个事件是android的基本事件,如果大多数程序监听,会大大的拖慢整个系统,所以android不鼓励我们在后台监听这两个事件。
2.因为有序广播的优先级问题。以上这些广播中,静态注册时,系统的优先级大于应用,并且系统阻止了广播的向下传播。又因在Android 的广播机制中,动态注册的优先级是要高于静态注册优先级的。故用动态注册代替静态注册。
3.系统安全问题。

4.不能静态注册的广播还有以下几个:

android.intent.action.SCREEN_ON
android.intent.action.SCREEN_OFF
android.intent.action.BATTERY_CHANGED
android.intent.action.CONFIGURATION_CHANGED

5.解决方式(以android.intent.action.SCREEN_ON为例):
动 态注册不能放到activity中,因为动态注册必须要在activity消亡的时候调用unregisterReceiver,会随着activity 的解锁消失而不能再接收广播。一般的办法是在activity起来后马上start一个service,这个service里动态注册一 个broadcastreceiver,broadcastreceiver里接收到SCREEN_ON消息后启动锁屏activitty 为了保证 broadcastreceiver任何时候都可以接收到SCREEN_ON,service必须常驻在系统内,所以要接收开机消息 android.intent.action.BOOT_COMPLETED。

  • 11
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (Intent.ACTION_TIME_TICK.equals(action) || Intent.ACTION_TIME_CHANGED.equals(action) || Intent.ACTION_TIMEZONE_CHANGED.equals(action) || Intent.ACTION_LOCALE_CHANGED.equals(action)) { if (Intent.ACTION_LOCALE_CHANGED.equals(action) || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) { // need to get a fresh date format mDateFormat = null; } updateClock(); } } }; public DateView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_TIME_TICK); filter.addAction(Intent.ACTION_TIME_CHANGED); filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); filter.addAction(Intent.ACTION_LOCALE_CHANGED); mContext.registerReceiver(mIntentReceiver, filter, null, null); updateClock(); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mDateFormat = null; // reload the locale next time mContext.unregisterReceiver(mIntentReceiver); } protected void updateClock() { if (mDateFormat == null) { final String dateFormat = getContext().getString(R.string.system_ui_date_pattern); final Locale l = Locale.getDefault(); final String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString()); mDateFormat = new SimpleDateFormat(fmt, l); } mCurrentTime.setTime(System.currentTimeMillis()); final String text = mDateFormat.format(mCurrentTime); if (!text.equals(mLastText)) { setText(text); mLastText = text; } } }
06-08

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值