2

我需要知道,如何在 android java 代码中处理队列。当队列不为空时,我想触发一些方法。任何人都可以就此提供建议...</p>

目前我已经实现了一个定时器任务。这个类经常看到队列状态。当队列不为空时,它将触发该方法。

我想知道有任何替代方法可以做到这一点..

公共类 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;
    }       
} 

}

4

2 回答 2

2

您可以使用Queue对象来创建和管理队列,并使用Observer接口在您将元素添加到队列时触发事件来完成此操作。

这里有一个例子。

于 2013-01-14T11:08:54.627 回答
0

代替 Timer 线程,使用普通线程和阻塞队列:

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);
于 2013-01-14T12:07:10.220 回答