Advertisement
Guest User

PollingService.Java

a guest
Nov 18th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. package com.color.speechbubble;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5.  
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.HttpStatus;
  8. import org.apache.http.StatusLine;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.HttpClient;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13.  
  14. import android.app.Service;
  15. import android.content.Intent;
  16. import android.net.ConnectivityManager;
  17. import android.os.AsyncTask;
  18. import android.os.IBinder;
  19. import android.os.PowerManager;
  20. import android.os.PowerManager.WakeLock;
  21. import android.util.Log;
  22.  
  23. public class PollingService extends Service {
  24.     private WakeLock mWakeLock;
  25.    
  26.     public void PollingService() {
  27.         Log.wtf("I AM ACTUALLY RUNNING", "IN THE Constructor!!!!");
  28.     }
  29.    
  30.     @Override
  31.     public IBinder onBind(Intent intent) {
  32.         return null;
  33.     }
  34.    
  35.     private void handleIntent(Intent intent) {
  36.         // obtain the wake lock
  37.         PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
  38.         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MWAKELOGTAG");
  39.         mWakeLock.acquire();
  40.        
  41.         // Check the global background data setting
  42.         ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
  43.         if (!cm.getBackgroundDataSetting()) {
  44.             stopSelf();
  45.             return;
  46.         }
  47.        
  48.         // Do the actual work, in a separate thread
  49.         new PollTask().execute();
  50.     }
  51.    
  52.     private class PollTask extends AsyncTask<String, String, String> {
  53.         // TODO: insert the polling in here.
  54.         private String responseString = null;
  55.        
  56.         @Override
  57.         protected String doInBackground(String... uri) {
  58.             Log.wtf("QQQQQQQQQQQQQQQ", "IN THE PollTask!!!!");
  59.            
  60.             HttpClient httpclient = new DefaultHttpClient();
  61.             HttpResponse response;
  62.            
  63.             try {
  64.                 response = httpclient.execute(new HttpGet(uri[0]));
  65.                 StatusLine statusLine = response.getStatusLine();
  66.                 if(statusLine.getStatusCode() == HttpStatus.SC_OK){
  67.                     ByteArrayOutputStream out = new ByteArrayOutputStream();
  68.                     response.getEntity().writeTo(out);
  69.                     out.close();
  70.                     responseString = out.toString();
  71.                 } else{
  72.                     //Closes the connection.
  73.                     response.getEntity().getContent().close();
  74.                     throw new IOException(statusLine.getReasonPhrase());
  75.                 }
  76.             } catch (ClientProtocolException e) {
  77.                 //TODO Handle problems..
  78.             } catch (IOException e) {
  79.                 //TODO Handle problems..
  80.             }
  81.             return responseString;
  82.         }
  83.        
  84.         @Override
  85.         protected void onPostExecute(String result) {
  86.             // handle data
  87.             // TODO: handle json using the GSON lib.
  88.             Log.wtf("POLLINGGGGGGGGGGG", result);
  89.             // TODO: Get the data back to the chat screen
  90.             stopSelf();
  91.         }
  92.     }
  93.    
  94.     @Override
  95.     public int onStartCommand(Intent intent, int flags, int startId) {
  96.         handleIntent(intent);
  97.         return START_NOT_STICKY;
  98.     }
  99.    
  100.     public void onDestroy() {
  101.         super.onDestroy();
  102.         mWakeLock.release();
  103.     }
  104.  
  105.     public PollingService() {
  106.         // TODO Auto-generated constructor stub
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement