这个广播动作是以每分钟一次的形式发送。但你不能通过在manifest.xml里静态注册,只能在代码里通过registerReceiver()方法动态注册。个人觉得可以用来做闹钟,或者开启被关闭的服务之类的、
1.广播的创建:
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)) {
// to do
}
}
};
3.检测服务是否在运行
public static boolean isServiceRunning(Class<?> serviceClass) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = 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;
}