安卓时间相关的广播(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。

@Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (!mAttached) { mAttached = true; 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_CONFIGURATION_CHANGED); filter.addAction(Intent.ACTION_USER_SWITCHED); getContext().registerReceiver(mIntentReceiver, filter, null, getHandler()); } // NOTE: It's safe to do these after registering the receiver since the receiver always runs // in the main thread, therefore the receiver can't run before this method returns. // The time zone may have changed while the receiver wasn't registered, so update the Time mCalendar = Calendar.getInstance(TimeZone.getDefault()); // Make sure we update to the current time postDelayed(new Runnable() { @Override public void run() { updateClock(); } }, 1000); } private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { String tz = intent.getStringExtra("time-zone"); mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz)); if (mClockFormat != null) { mClockFormat.setTimeZone(mCalendar.getTimeZone()); } } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { final Locale newLocale = getResources().getConfiguration().locale; if (! newLocale.equals(mLocale)) { mLocale = newLocale; mClockFormatString = ""; // force refresh } } updateClock(); } }; final void updateClock() { if (mDemoMode) return; mCalendar.setTimeInMillis(System.currentTimeMillis()); setText(getSmallTime()); }状态栏秒数还是没有变化,使用getSmallTimeWithSeconds()报错
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值