码迷,mamicode.com
首页 > 移动开发 > 详细

Android Umeng PushService

时间:2014-12-29 12:13:14      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

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);
		}

	}
}

Android Umeng PushService

标签:

原文地址:http://blog.csdn.net/xufeifandj/article/details/42235425

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!