Note that there are some explanatory texts on larger screens.

plurals
  1. POFiguring out alarm manager
    primarykey
    data
    text
    <p>I am trying to create an app that will send some data to a webserver after x mins. I was told that an alarm manager would be the best solution and just call the same alarm function with an extra x mins inside the alarm so that it will constantly send the data even in the background.</p> <p>However I have only seen alarms open intents within the project. Does that mean it will switch to the new intent after x mins or will everything work in the background? </p> <p>The background data should be sent without having to switch between intents so I would prefer it to just call a function inside one of my activities. How should I go about doing this?</p> <p>Here is the function I would like to call every x mins.</p> <p>Thanks</p> <pre><code>public class updateloc extends AsyncTask&lt;Void, Void, Void&gt; { protected void onPostExecute(String response) { if (response != null) { // check if this does anything later } progressDialog.dismiss(); } @Override protected void onPreExecute() { super.onPreExecute(); progressDialog = ProgressDialog.show(ImTracking.this, "", "Updating Data..."); } @Override protected Void doInBackground(Void... arg0) { SharedPreferences prefs = getSharedPreferences("Settings", 0); final String id = prefs.getString("ID", ""); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost( "http://iphone-radar.com/gps/gps_locations"); JSONObject holder = new JSONObject(); try { holder.put("id", id); LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); String bestProvider = locationManager.getBestProvider(criteria, false); LocationListener loc_listener = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { } }; try { Looper.prepare(); locationManager.requestLocationUpdates(bestProvider, 0, 0, loc_listener); } catch (Exception e) { e.printStackTrace(); } Location location = locationManager .getLastKnownLocation(bestProvider); Calendar c = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat( "hh:mmaa MM/dd/yyyy"); holder.put("time", sdf.format(c.getTime())); holder.put("time_since_epoch", System.currentTimeMillis()); try { holder.put("lat", location.getLatitude()); holder.put("lon", location.getLongitude()); } catch (NullPointerException e) { try { holder.put("lat", -1.0); holder.put("lon", -1.0); } catch (JSONException e1) { e1.printStackTrace(); } } StringEntity se = new StringEntity(holder.toString()); httpost.setEntity(se); httpost.setHeader("Accept", "application/json"); httpost.setHeader("Content-type", "application/json"); ResponseHandler responseHandler = new BasicResponseHandler(); String response = httpclient.execute(httpost, responseHandler); org.json.JSONObject obj; obj = new org.json.JSONObject(response); } catch (JSONException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } progressDialog.dismiss(); return null; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload