如何在android中处理队列?爪哇 [英] how to handle a queue in android?? java

查看:102
本文介绍了如何在android中处理队列?爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何在android java代码中处理队列.我想在队列不为空时触发某种方法.任何人都可以对此提出建议吗?

I need to know, how to handle a queue in android java code. I want to fire some method when queue is not empty. Can anyone give advice about this…

目前,我已经实现了计时器任务.此类经常看到队列状态.当队列不为空时,将触发该方法.

Currently I have implemented a timer task. This class frequently sees the queue status. When queue is not empty it will fire the method.

我想知道还有其他方法可以做到这一点.

I want to know have any alternative ways to do this..

公共类GSMLocationTask扩展了TimerTask { 处理程序TDGetDeviceLocHandler;

public class GSMLocationTask extends TimerTask { Handler 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;
    }       
} 

}

推荐答案

您可以使用队列对象来创建和管理队列,以及

You can accomplish this using a Queue object to create and manage the queue, and the Observer interface to fire the event when you add an element to the queue.

此处的示例.

这篇关于如何在android中处理队列?爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆