The first step of my app is working: when a button is clicked it makes an http request, does a JSON parse of the result and displays and stores the data. I now want to be able to schedule events so that the same thing happens regularly in the background - and I can't get it to work. I've looked at a couple of tutorials on creating broadcastreceivers and alarms (http://justcallmebrian.com/?p=129 and http://mobile.tutsplus.com/tutorials/android/android-fundamentals-scheduling-recurring-tasks/) and have replicated in a separate app the simple version that just produces a toast message when the scheduled event occurs. But when I try to combine the 2 things (schedule an event to make the http request) the complexity is defeating me. If anyone can give me some guidance I would appreciate it.
The first step (that works) starts with a button click that runs a method that has just 2 lines.
urlStr = "http://data_service_site?parm=1";
new AsyncNetworkConnection().execute(urlStr);
The main activity then has this inline class.
class AsyncNetworkConnection extends AsyncTask<String, String, String>
{
@Override
protected void onPostExecute(String result) {
//working JSON code here
//working code to display & store data here
}
@Override
protected String doInBackground(String... arg0) {
String results = null;
try {
results = fetchHTML(arg0[0]);
} catch (ClientProtocolException e) {
String msg = getResources().getString(R.string.str_html_error, e.getMessage());
Log.e(TAG, "Http error", e);
} catch (Exception e) {
String msg = getResources().getString(R.string.str_html_error, e.getMessage());
Log.e(TAG, "Http connection error", e);
}
return results;
}
private String fetchHTML(String urlStr) throws URISyntaxException, ClientProtocolException, IOException, Exception
{
DefaultHttpClient httpclient = null;
URI serviceUri = new URI(urlStr);
String result;
try {
HttpGet getRequest = new HttpGet(serviceUri);
ResponseHandler<String> handler = new BasicResponseHandler();
httpclient = new DefaultHttpClient();
result = httpclient.execute(getRequest, handler);
Log.i(TAG, "Put to Service. Result: " + result);
} catch (Exception e) {
throw e;
} finally {
if(null != httpclient){
httpclient.getConnectionManager().shutdown();
}
}
return result;
}
}
The above works; what follows doesn't. To try a simple method of scheduling the same task, I've added another button which runs this method.
public void setOneOffAlarm(View v) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 2);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
And I have this alarmreceiver class. I had problems creating it as a separate class file so have it as an inline class in the same activity. I first had it trying to do the http request then tried simplifying it to just display a toast message - neither work.
public class AlarmReceiver extends BroadcastReceiver {
private static final String DEBUG_TAG = "AlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Intent downloader = new Intent(context, AsyncNetworkConnection.class);
//downloader.setData(Uri
//.parse("http://data_service_site?parm=1"));
//Try just showing toast:
String message = ("alarm_message");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
context.startService(downloader);
}
}
Any help appreciated.