android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到

本文介绍在Android中如何正确监听ACTION_TIME_TICK事件。通过动态注册而非静态注册方式实现每分钟触发一次的监听机制。

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

android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到

今天做android上的消息推送,启动了一个独立service,然后在里面监听系统的ACTION_TIME_TICK消息,即tick就是以分钟为单位,每分钟都会监听到一次,

按照网上说的在androidmanifast.xml里加入了

 

复制代码
 <receiverandroid:name="com.xxx.xxx.TimeChangeReceiver">

        <intent-filterandroid:name="android.intent.action.ACTION_TIME_TICK"></intent-filter>

    </receiver>
复制代码

然后也写了个继承自BroadcastReceiver的类叫做TimeChangeReceiver与上面对应,结果就是无法监听到这个事件,

花了半个小时无果,google的api页面又被墙了,于是尝试使用动态添加的方式,即在程序里需要的地方直接new一个receiver出来 ,果断删掉这个类,和xml里的上面那一段,直接在service的onCreate里写如下代码:

1 IntentFilter filter=new IntentFilter();
2         filter.addAction(Intent.ACTION_TIME_TICK);
3         registerReceiver(receiver,filter);
复制代码
 1 private final BroadcastReceiver receiver = new BroadcastReceiver() {
 2         @Override
 3           public void onReceive(Context context, Intent intent) {
 4               String action = intent.getAction();
 5                 if (action.equals(Intent.ACTION_TIME_TICK)) {
 6 
 7                   //do what you want to do ...13                     
14                 }
15           }
16     };
复制代码

成功了。

 
#1楼 2014-03-03 15:29murphykwu  

 

google的api里面讲ACTION_TIME_TICK只能够动态创建监听。不能放在androidmanifest.xml里面设定。
 

 

#2楼 2015-03-10 22:32热火火  
1楼说的对。

转载于:https://www.cnblogs.com/ZhuRenWang/p/5362789.html

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

余额充值