Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.color.speechbubble;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.StatusLine;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import android.app.Service;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.os.AsyncTask;
- import android.os.IBinder;
- import android.os.PowerManager;
- import android.os.PowerManager.WakeLock;
- import android.util.Log;
- public class PollingService extends Service {
- private WakeLock mWakeLock;
- public void PollingService() {
- Log.wtf("I AM ACTUALLY RUNNING", "IN THE Constructor!!!!");
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- private void handleIntent(Intent intent) {
- // obtain the wake lock
- PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
- mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MWAKELOGTAG");
- mWakeLock.acquire();
- // Check the global background data setting
- ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
- if (!cm.getBackgroundDataSetting()) {
- stopSelf();
- return;
- }
- // Do the actual work, in a separate thread
- new PollTask().execute();
- }
- private class PollTask extends AsyncTask<String, String, String> {
- // TODO: insert the polling in here.
- private String responseString = null;
- @Override
- protected String doInBackground(String... uri) {
- Log.wtf("QQQQQQQQQQQQQQQ", "IN THE PollTask!!!!");
- HttpClient httpclient = new DefaultHttpClient();
- HttpResponse response;
- try {
- response = httpclient.execute(new HttpGet(uri[0]));
- StatusLine statusLine = response.getStatusLine();
- if(statusLine.getStatusCode() == HttpStatus.SC_OK){
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- response.getEntity().writeTo(out);
- out.close();
- responseString = out.toString();
- } else{
- //Closes the connection.
- response.getEntity().getContent().close();
- throw new IOException(statusLine.getReasonPhrase());
- }
- } catch (ClientProtocolException e) {
- //TODO Handle problems..
- } catch (IOException e) {
- //TODO Handle problems..
- }
- return responseString;
- }
- @Override
- protected void onPostExecute(String result) {
- // handle data
- // TODO: handle json using the GSON lib.
- Log.wtf("POLLINGGGGGGGGGGG", result);
- // TODO: Get the data back to the chat screen
- stopSelf();
- }
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- handleIntent(intent);
- return START_NOT_STICKY;
- }
- public void onDestroy() {
- super.onDestroy();
- mWakeLock.release();
- }
- public PollingService() {
- // TODO Auto-generated constructor stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement