Figuring out alarm manager
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.
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?
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?
Here is the function I would like to call every x mins.
Thanks
public class updateloc extends AsyncTask<Void, Void, Void> {
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();
Stri开发者_Go百科ng 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;
}
}
If you want this to work in the background, you need to create a service. Services can be started using intents, so you can start your service using the AlarmManager. Have a look at IntentService, you just need to move what you are currently doing in your AsyncTask
in the servcie's handleIntent()
method.
精彩评论