how to handle a queue in android?? java
11,630
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.
An example here.
Author by
Supun Chamikara
Updated on June 04, 2022Comments
-
Supun Chamikara almost 2 years
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..
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; } }
}