public class QueryManager { private static Context context; private static Intent intent; private static boolean stopFlag; private static Runnable runnableQueryService; private static Handler queryHandler;
queryHandler .postDelayed(runnableQueryService, dylaySeconds*1000);//第一次调用延迟
queryHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); String strJson = msg.getData().getString("data"); processServiceQueryData(strJson); if(!stopFlag) { queryHandler.postDelayed(runnableQueryService, 20 * 1000); } }};
QueryNewCountManager.runnableQueryService = new Runnable(){ @Override public void run() {//调用服务进行一次查询 Intent intent = new Intent(context, QueryService.class); if(queryHandler == null){ return; } QueryService.setHandler(queryHandler); context.startService(intent); }};
QueryNewCountManager.intent = new Intent(context, QueryFireCountService.class);
(handler)queryHandler --> (runnable)runnableQueryService --> (service)QueryService(带上queryHandler,查询完后,sendmessage 给 queryHandler) queryHandler处理QueryService发来的service。然后重新调用runnableQueryService 。 如此反复进行心跳查询。
时间: 2025-01-31 04:03:20