2013-01-14 113 views
1

我需要知道,如何處理android java代碼中的隊列。我想在隊列不爲空時激發一些方法。任何人都可以給這個建議...如何在android中處理隊列? java

目前我已經實現了一個計時器任務。這個類經常看到隊列狀態。當隊列不爲空時,它將激發該方法。

我想知道有什麼其它方法來做到這一點..

公共類GSMLocationTask擴展的TimerTask { 處理器TDGetDeviceLocHandler;

int myLatitude, myLongitude; 
int cid; 
int lac; 
double latitude; 
double longitude; 
TelephonyManager telephonyManager; 
GsmCellLocation cellLocation; 
LocationSendTask lst; 

public GSMLocationTask(LocationSendTask locSendtask) { 
    // TODO Auto-generated constructor stub 
    this.lst = locSendtask; 
    this.telephonyManager = (TelephonyManager)TrackDriodApplication.getAppContext().getSystemService(TrackDriodApplication.getAppContext().TELEPHONY_SERVICE); 
    this.cellLocation = (GsmCellLocation) telephonyManager.getCellLocation(); 
} 


@Override 
public void run() 
{ 
    Log.d("GSMLocationTask", "GSM Location task Start run..."); 

     //cid = cellLocation.getCid(); 
     //lac = cellLocation.getLac(); 
     cid = 256229;//cellLocation.getCid(); 
     lac = 30310;//cellLocation.getLac(); 
     try 
     { 
      //new TDGetDeviceLocation().execute(null, null, null); 

      if(RqsLocation(cid, lac)) 
      { 
       latitude = (float)myLatitude/1000000; 
       longitude = (float)myLongitude/1000000; 
       Log.d("GSMLocationTask", "Lat :" +latitude +" Long :"+longitude); 
      } 

      DataTransaction dtra = new DataTransaction(); 
      ServerSettings ss = new ServerSettings(); 
      ss = dtra.getDeviceSettings(TrackDriodApplication.getAppContext()); 
      String deviceID = String.valueOf(ss.getDeviceID()); 
      Log.d("GSMLocationTask", "locationSend obj create"); 
      LocationSend loc = new LocationSend(); 
      Log.d("GSMLocationTask", "set lat"); 
      loc.setLatitude(Double.toString(latitude)); 
      Log.d("GSMLocationTask", "set long"); 
      loc.setLongitude(Double.toString(longitude)); 
      Log.d("GSMLocationTask", "set devid"); 
      loc.setDeviceID(deviceID); 
      Log.d("GSMLocationTask", "set set datetime"); 
      loc.setDateTime(getCurrentTime()); 

      Log.d("GSMLocationTask", "add loc to queue sart"); 
      lst.addLocationToQueue(loc); 
      Log.d("GSMLocationTask", "add loc to queue end"); 
     } 
     catch (Exception e) 
     { 
     } 


} 
private static String getCurrentTime() 
{ 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    String currentDateandTime = sdf.format(new Date()); 
    return currentDateandTime; 
} 

private Boolean RqsLocation(int cid, int lac) 
{   
    Log.d("GSMLocationTask", "call ReqLocation"); 
      Boolean result = false; 
      String urlmmap = "http://www.google.com/glm/mmap"; 

       try { 
        Log.d("GSMLocationTask", "start try.."); 
       URL url = new URL(urlmmap); 
       URLConnection conn = url.openConnection(); 
       HttpURLConnection httpConn = (HttpURLConnection) conn;  
       httpConn.setRequestMethod("POST"); 
       httpConn.setDoOutput(true); 
       httpConn.setDoInput(true); 
       httpConn.connect(); 

       OutputStream outputStream = httpConn.getOutputStream(); 
       WriteData(outputStream, cid, lac); 

       InputStream inputStream = httpConn.getInputStream(); 
       DataInputStream dataInputStream = new DataInputStream(inputStream); 

       dataInputStream.readShort(); 
       dataInputStream.readByte(); 
       int code = dataInputStream.readInt(); 
       if (code == 0) 
       { 
        myLatitude = dataInputStream.readInt(); 
        myLongitude = dataInputStream.readInt(); 

        result = true; 
       } 
       Log.d("GSMLocationTask", "end try.."); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Log.d("GSMLocationTask", "return result :" +result); 
     return result; 

} 

private void WriteData(OutputStream out, int cid, int lac) throws IOException   
    {  
     DataOutputStream dataOutputStream = new DataOutputStream(out); 
     dataOutputStream.writeShort(21); 
     dataOutputStream.writeLong(0); 
     dataOutputStream.writeUTF("en"); 
     dataOutputStream.writeUTF("Android"); 
     dataOutputStream.writeUTF("1.0"); 
     dataOutputStream.writeUTF("Web"); 
     dataOutputStream.writeByte(27); 
     dataOutputStream.writeInt(0); 
     dataOutputStream.writeInt(0); 
     dataOutputStream.writeInt(3); 
     dataOutputStream.writeUTF("");   
     dataOutputStream.writeInt(cid); 
     dataOutputStream.writeInt(lac);    
     dataOutputStream.writeInt(0); 
     dataOutputStream.writeInt(0); 
     dataOutputStream.writeInt(0); 
     dataOutputStream.writeInt(0); 
     dataOutputStream.flush();  
    } 

class TDGetDeviceLocation extends AsyncTask<Object, Object, Object>{ 

    @Override 
    protected Object doInBackground(Object... params) { 
     try 
     {    
      Log.d("GSMLocationTask", "doInBackground start........"); 
      if(RqsLocation(cid, lac)) 
      { 
       latitude = (float)myLatitude/1000000; 
       longitude = (float)myLongitude/1000000; 
      } 

      return null; 
     } 
     catch (Exception e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    }  
} 

}

+2

請添加一些代碼,並解釋該問題的好一點 – thepoosh

+0

它有點複雜的代碼.. –

回答

2

您可以使用Queue對象來創建和管理隊列做到這一點,和Observer接口時,你一個元素添加到隊列觸發事件。

示例here

+0

感謝ü..我會嘗試它... –

0

而是計時器線程的,使用普通絲和阻塞隊列:

class Worker extends Thread { 
    BlockingQueue<LocationSend> queue = new LinkedBlockingQueue<LocationSend>(); 

    public void run() { 
    for (;;) { 
     LocationSend loc=queue.take(); 
     someMethod(loc): 
    } 
    } 
} 
... 
Worker worker=new Worker(); 
worker.start(); 
... 
LocationSend loc = new LocationSend(); 
worker.queue.put(loc);