upsidedown

myservice

Feb 20th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. package com.example.adnan_test;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.net.UnknownHostException;
  8. import java.util.Timer;
  9. import java.util.TimerTask;
  10.  
  11. import com.example.adnan_test.Json;
  12.  
  13.  
  14. import android.app.Service;
  15. import android.content.Context;
  16. import android.content.Intent;
  17. import android.os.IBinder;
  18. import android.telephony.TelephonyManager;
  19. import android.util.Log;
  20. import android.widget.Toast;
  21.  
  22.  
  23. public class Myservice extends Service {
  24.  
  25.     int counter = 0;
  26.     static final int UPDATE_INTERVAL = 10000;
  27.     private Timer timer = new Timer();
  28.     @Override
  29.     public IBinder onBind(Intent arg0) {
  30.         // TODO Auto-generated method stub
  31.         return null;
  32.     }
  33.     public int onStartCommand(Intent intent, int flags, int startId ) {
  34.         // We want this service to continue running until it is explicitly
  35.         // stopped, so return sticky.
  36.         timer.scheduleAtFixedRate(new TimerTask(){
  37.            
  38.         @SuppressWarnings("static-access")
  39.         public void run(){
  40.          String lati = null,longi=null;
  41.         //Toast.makeText(this,"Service Started", Toast.LENGTH_LONG).show();
  42.         // Toast q = new Toast(getApplicationContext());
  43.     //   Toast.makeText(getApplicationContext(), "service started", q.LENGTH_LONG).show();
  44.          Log.d(Constants.LOG, "Service Runs");
  45.  
  46.         Tracker t = new Tracker(Myservice.this);
  47.         if(t.canGetLocation())
  48.         {
  49.             lati=Double.toString(t.getlat());
  50.             longi=Double.toString(t.getlon());
  51.              Log.d(Constants.LOG, "GPS Enabled");
  52.         // Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_LONG).show();
  53.                  
  54.         }
  55.         else
  56.         {
  57.            
  58.             //Toast.makeText(getApplicationContext(), "GPS Disabled", Toast.LENGTH_LONG).show();
  59.              Log.d(Constants.LOG, "GPS Disabled");
  60.  
  61.         }
  62.        
  63.         TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  64.         final String id = mngr.getDeviceId();
  65.        
  66.        
  67.         /*Json j= new Json();      
  68.         j.send(lati,longi,id);
  69.          Log.d(Constants.LOG, "Data send attempted");*/
  70.        
  71.         try{
  72.         String url="http://testapp1pranav.appspot.com/gettaxi?id="+id+"&lat="+lati+"&lon="+longi;
  73.        
  74.         URL obj = new URL(url);
  75.         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  76.  
  77.         // optional default is GET
  78.         con.setRequestMethod("POST");
  79.  
  80.         //add request header
  81.         con.setRequestProperty("User-Agent", "Mozilla/5.0");
  82.  
  83.         int responseCode = con.getResponseCode();
  84.        
  85.         Log.d(Constants.LOG, "Response Code : " + responseCode);
  86.  
  87.  
  88.         BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream()));
  89.         String input;
  90.         StringBuffer buff = new StringBuffer();
  91.  
  92.         while ((input = in.readLine()) != null)
  93.         {
  94.             buff.append(input);
  95.         }
  96.         in.close();
  97.        
  98.         String rply =buff.toString();
  99.                  
  100.            if(rply == "SUCCESS")
  101.            {
  102.                  Log.d(Constants.LOG, "POST Success");
  103.  
  104.            }
  105.            else
  106.            {
  107.                Log.d(Constants.LOG, "Post Failed");
  108.            }
  109.     }
  110.    
  111.     catch (UnknownHostException h)
  112.     {
  113.          Log.e(Constants.LOG, "Internet connection not available");
  114.  
  115.     }
  116.  catch(Exception e)
  117.     {
  118.      Log.e(Constants.LOG, "OTHER ERROR");
  119.     }
  120.  
  121.         }
  122.        
  123.         }, 0, UPDATE_INTERVAL);
  124.         return START_STICKY;
  125.         }
  126.         @Override
  127.         public void onDestroy() {
  128.            
  129.         super.onDestroy();
  130.         if (timer != null){
  131.             timer.cancel();
  132.              Log.d(Constants.LOG, "Service Stopped");
  133.  
  134.             }
  135.         }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment