Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Umeng PushService

Android Umeng PushService

編輯:關於Android編程

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

	}
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved