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

开发守护进程或者天气预报一些定期检查服务是否存在操作时我们需要用到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;

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值