android intent.action time tick,Intent.ACTION_TIME_TICK的正确用法

这篇博客介绍了如何利用ACTION_TIME_TICK广播来每分钟检查服务是否正在运行。ACTION_TIME_TICK是一个特殊的Intent,不能在manifest.xml中注册,而需要在代码中通过registerReceiver()动态注册。文中给出了具体的代码示例,包括创建IntentFilter,动态注册接收器以及检查服务运行状态的方法。

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

开发守护进程或者天气预报一些定期检查服务是否存在操作时我们需要用到ACTION_TIME_TICK。看看文档里面是怎么说ACTION_TIME_TICK的。

在众多的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()方法注册。

如此我们就知道如何操作了,在xml配置肯定是不行的。只能用过代码动态注册。IntentFilter filter=new IntentFilter();

filter.addAction(Intent.ACTION_TIME_TICK);

registerReceiver(receiver,filter); private final BroadcastReceiver receiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (action.equals(Intent.ACTION_TIME_TICK)) {

//do what you want to do ...13

}

}

};检测服务是否在运行中:public static boolean isServiceRunning(Class> serviceClass) {

ActivityManager activityManager = (ActivityManager) context

.getSystemService(Context.ACTIVITY_SERVICE);

ListserviceList = activityManager

.getRunningServices(Integer.MAX_VALUE);

if (serviceList == null || serviceList.size() == 0)

return false;

for (RunningServiceInfo info : serviceList) {

if (info.service.getClassName().equals(serviceClass.getName()))

return true;

}

return false;

}

现在,你能收到这个广播了!赶紧更新吧~~~

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

余额充值