安卓Intent.ACTION_TIME_TICK 广播

本文详细解释了Android系统中Intent.ACTION_TIME_TICK广播的特性,强调其每分钟触发一次的特点,并指出该广播接收需要通过代码动态注册,无法在清单文件中配置。文章通过示例展示了如何创建BroadcastReceiver类并实现onReceive方法来监听此特定广播。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Intent.ACTION_TIME_TICK 广播需要动态注册,不能在清单文件配置.

 

  1. TimeReceiver mBroadcastReceiver = new TimeReceiver();  
  2. IntentFilter intentFilter = new IntentFilter();
  3. intentFilter.addAction(Intent.ACTION_TIME_TICK);  
  4. registerReceiver(mBroadcastReceiver, intentFilter);  

在 TimeReceiver 监听广播接收。

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. public class TimeReceiver extends BroadcastReceiver {  
  2.   
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.         if(Intent.ACTION_TIME_TICK.equals(intent.getAction())) {  
  6.             //todo...  
  7.         }  
  8.     }  
  9. }  

在众多的Intent的action动作中,Intent.ACTION_TIME_TICK是比较特殊的一个,根据SDK描述:


Broadcast Action: The current time has changed. Sent every minute. You can not receive this through components declared in manifests, only by exlicitly registering for it withContext.registerReceiver()


意思是说这个广播动作是以每分钟一次的形式发送。但你不能通过在manifest.xml里注册的方式接收到这个广播,只能在代码里通过registerReceiver()方法注册。

 

转载于:https://www.cnblogs.com/berylqliu/p/6261509.html

@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、付费专栏及课程。

余额充值