Currently, i have built a service to do httpRequest to my php server every 15 seconds. The code works well but the problem is when i close the app the httpRequest is stop working and does not send data anymore. what i want to do is when i close the app, the httpRequest still running. what should i change in my code?
here is the code i have been working on...
public class NotificationService extends Service {
public String response;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "on start command", Toast.LENGTH_SHORT).show();
new MyAsyncTask().execute("muhammad sappe");
return START_NOT_STICKY;
}
private class MyAsyncTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0]);
return null;
}
@Override
protected void onPostExecute(String result) {
new Handler().postDelayed(new Runnable() {
public void run() {
new MyAsyncTask().execute("muhammad sappe");
}
}, 15000);
super.onPostExecute(result);
}
}
public void postData(String MyName){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mydomain/getData.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("action", MyName));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response = httpclient.execute(httppost, responseHandler);
}catch(ClientProtocolException e){
// TODO Auto-generated catch block
}catch(IOException e) {
// TODO Auto-generated catch block
}
}
public void onDestroy() {
super.onDestroy();
}
}
Aucun commentaire:
Enregistrer un commentaire