ThreadRunner.java :  » App » traindroid » com » traindroids » updater » Android Open Source

Android Open Source » App » traindroid 
traindroid » com » traindroids » updater » ThreadRunner.java
package com.traindroids.updater;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Locale;



import android.location.Address;
import android.location.Geocoder;
import android.util.Log;
import android.widget.Toast;

public class ThreadRunner extends Thread{
  TraindroidsUpdater parent;
  private boolean threadStatus=true;
  private String path,id;
  public URL url;
  int sleepTime;
  public ThreadRunner(TraindroidsUpdater parent,String id,String path,int sleepTime){
    this.parent=parent;
    this.id=id;
    this.path=path;
    this.sleepTime=sleepTime;
  }
  @Override
  public void run() {
    double longitude=0,latitude=0;
    //Error counter akan menghitung berapa kali terjadi error secara berturut-turut
    //Jika error lebih dari 3 secara berturut-turut, maka perulangan akan berhenti
    int errorCounter=0;
    while(threadStatus){
      errorCounter=0;
      longitude=this.parent.getLongitudeValue();
      latitude=this.parent.getLatitudeValue();
      if(!(longitude==0)&&!(latitude==0)){
        try{
          errorCounter=0;
          this.updateToWebservice(longitude,latitude,
              this.parent.getSpeedValues());
        }
        catch(Exception e){
          errorCounter++;
          Toast.makeText(null, "Koneksi bermasalah.\nError Counter "+errorCounter,
              Toast.LENGTH_SHORT).show();
        }        
      }
      try {
        Thread.sleep(this.sleepTime);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      if(errorCounter>=3){
        this.threadStatus=false;
      }
    }
    
  }
  
  public void stopThread(){
    this.threadStatus=false;
  }
  
  public void updateToWebservice(double longitude,double latitude,float speed) throws Exception{
    String fullpath=this.path+"/"+this.id+"/"+latitude+"/"+longitude+"/"+speed;
    this.url= new URL(fullpath);
    this.url.openConnection().getInputStream();
    Log.i("Thread Runner ", fullpath);
  }
  
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.