public class PushServices extends UmengBaseIntentService { private PushAgent mPushAgent; private ScheduledExecutorService service; private ScheduledFuture scheduledFuture; private int i = 1; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { Toast.makeText(getApplicationContext(), "定时:"+msg.what, Toast.LENGTH_SHORT).show(); }; }; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); try { timeTask(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * * @Title: timeTask * @Description: TODO 定时更新任务,一集定时上传 * @return void */ public void timeTask() throws InterruptedException, ExecutionException { // TODO Auto-generated method stub if (service == null) { service = Executors.newScheduledThreadPool(1); } if (scheduledFuture == null) { scheduledFuture = service.scheduleAtFixedRate(new UploadData(), 0, 5, TimeUnit.SECONDS); } } @Override protected void onMessage(Context context, Intent intent) { // TODO Auto-generated method stub super.onMessage(context, intent); try { String message = intent.getStringExtra(BaseConstants.MESSAGE_BODY); if (StringUtils.isEmpty(message)) { return; } UMessage msg = new UMessage(new JSONObject(message)); if (msg != null) { mPushAgent = PushAgent.getInstance(context); UmengMessageHandler messageHandler = new UmengMessageHandler(); messageHandler.dealWithNotificationMessage(context, msg); UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler(); mPushAgent.setMessageHandler(messageHandler); mPushAgent .setNotificationClickHandler(notificationClickHandler); UTrack.getInstance(context).trackMsgClick(msg); } } catch (Exception e) { } } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if (scheduledFuture != null) { scheduledFuture.cancel(true); scheduledFuture = null; } } // 上传任务 public class UploadData implements Runnable { @Override public void run() { // TODO Auto-generated method stub i++; Message message=Message.obtain(); message.what=i; handler.sendMessage(message); } } }
时间: 2024-10-14 02:58:45