【问题标题】:how to handle a queue in android?? java如何处理android中的队列?爪哇
【发布时间】:2013-01-14 10:25:12
【问题描述】:

我需要知道,如何在 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;
    }       
} 

}

【问题讨论】:

  • 请添加一些代码并更好地解释问题
  • 它有点复杂的代码..

标签: java android events queue timertask


【解决方案1】:

您可以使用Queue 对象来创建和管理队列,并使用Observer 接口在向队列中添加元素时触发事件。

一个例子here

【讨论】:

    【解决方案2】:

    使用普通线程和阻塞队列代替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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 2015-06-07
      • 2013-05-25
      相关资源
      最近更新 更多