Reputation: 49
Currently i have a class that sends data to the server on button press, i am looking to automate this service so that every 4 hrs it send the data to the server without pressing a button. The POST service is currently working in the background , Any pointers?? Do i have to implement a Timer no clue its out of my realm ?? Thanks in Advance !!
Button btnCreateUser = (Button) findViewById(R.id.btnSend);
// button click event
btnCreateUser.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating new product in background thread
new CreateNewUser().execute();
}
});
}
class CreateNewUser extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... args) {
String lat=latitude, lng=longitude;
SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
String newtime = sdfDateTime.format(new Date(System.currentTimeMillis()));
EditText username = (EditText) findViewById(R.id.myText);
String newString= (String)username.getText().toString();
String created_at=newtime;
String timezone="UTC";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url_create_user);
String responseBody = "";
HttpResponse response = null;
try {
String base64EncodedCredentials = "Basic " + Base64.encodeToString(
(username + ":" + created_at +":"+timezone+":"+lat+":"+lng).getBytes(),
Base64.NO_WRAP);
httppost.setHeader("Authorization", base64EncodedCredentials);
httppost.setHeader(HTTP.CONTENT_TYPE,"application/json");
JSONObject obj = new JSONObject();
obj.put("username", String.valueOf(newString));
obj.put("created_at",String.valueOf(newtime));
obj.put("timezone", String.valueOf(timezone));
obj.put("lat", String.valueOf(latitude));
obj.put("lng", String.valueOf(longitude));
httppost.setEntity(new StringEntity(obj.toString(), "UTF-8"));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
Log.d("response ok", "Reached Server ok");
startActivity(new Intent("com.xxxxx.CLEARSCREEN"));
} else {
Log.d("response not ok", "Something went wrong :/");
}
responseBody = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
return responseBody;
}
Upvotes: 1
Views: 231
Reputation: 8520
Use AlarmManager
(http://developer.android.com/reference/android/app/AlarmManager.html) to start your activity repeatedly in future.
Assuming that the activity you use to collect user data and send them to server is called YourActivity
just add some static methods as below for scheduling activity start.
You may call the schedule
method either on application start or from BOOT_COMPLETE
BroadcastReceiver
.
public static void schedule(Context context, String url,
long triggerAtMillis, long intervalMillis) {
final AlarmManager alarmManager =
(AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, triggerAtMillis,
intervalMillis, createPendingOperation(context));
}
public static void unschedule(Context context) {
final AlarmManager alarmManager =
(AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.cancel(createPendingOperation(context));
}
private static PendingIntent createPendingOperation(Context context) {
return PendingIntent.getActivity(
context, 0, new Intent(context, YourActivity.class),
PendingIntent.FLAG_CANCEL_CURRENT);
}
Upvotes: 1
Reputation: 1153
you can solve it by this code :
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
btnCreateUser.post(new Runnable() {
@Override
public void run() {
btnCreateUser .performClick();
}
});
}
}, 0, 14400); // for 4 hours
Upvotes: 1