-1
  • In my app, I have Fetch Data from URL, I want to write a simple Services, which continuously running background & give me Notification....
NILU
  • 5
  • 6
  • 1
    Your question is quite broad. Please be specific about what exactly is problematic for you rather than telling what you want to do. Do you want to know how to download data from URL, or how to write a service, or how to get a notification when a task gets completed? – VipulKumar Jan 15 '15 at 13:25
  • To create a background service, you can use `IntentService` or `Service` or `AsyncTask`. See http://stackoverflow.com/a/3028660/3922207 for more information. – Anggrayudi H Jan 15 '15 at 13:32
  • @ VipulKumar sir i know the Download data from URL Using AsyncTask, But i don't know in IntentService, – NILU Jan 17 '15 at 11:09
  • and how to schedule IntentService with AlarmManager? – NILU Jan 17 '15 at 11:16
  • n my app I have Getting Data from Url Using AsyncTask, these Url Everyday Uploaded or update some new data, then how is possible to download these data Once in day – NILU Jan 17 '15 at 16:57

1 Answers1

0

If you question is how to get data from a url in background while the main thread should not wait for the data to be fetched then you can look at Asynctask to achieve this. Code example below:

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
      String response = "";
      for (String url : urls) {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
          HttpResponse execute = client.execute(httpGet);
          InputStream content = execute.getEntity().getContent();

          BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
          String s = "";
          while ((s = buffer.readLine()) != null) {
            response += s;
          }

        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      return response;
    }

    @Override
    protected void onPostExecute(String result) {
      textView.setText(result);
    }
  }
Psypher
  • 10,717
  • 12
  • 59
  • 83
  • is it possible to schedule AsyncTask with AlarmManager – NILU Jan 17 '15 at 11:22
  • Yes you can. But I dont understand the need for it. Asynctask executes some tasks in a different thread similarly as the AlarmManger. If you want to execute something when your app is not running you can use AlarmManager, just copy all the code within the asynctask to the onReceive() of the BroadcastReceiver which is getting executed by the Alarm. – Psypher Jan 17 '15 at 11:32
  • sir In my app I have Getting Data from Url Using AsyncTask, these Url Everyday Uploaded or update some new data, then how is possible to download these data Once in day, – NILU Jan 17 '15 at 12:22