1

I want an example read data from web and out to textview with intent service.I read this http://www.vogella.com/articles/AndroidServices/article.html but download the file...i want store data to string for use in another function in main activity.Thanks.

public class DownloadService extends IntentService {

private int result = Activity.RESULT_CANCELED;
public String s = "";

public DownloadService() {
super("DownloadService");
}

// Will be called asynchronously be Android
@Override
protected void onHandleIntent(Intent intent) {
Uri data = intent.getData();
String urlPath = intent.getStringExtra("urlpath");

InputStream stream = null;
//FileOutputStream fos = null;
try {

  URL url = new URL(urlPath);
  stream = url.openConnection().getInputStream();
  InputStreamReader reader = new InputStreamReader(stream);
  //fos = new FileOutputStream(output.getPath());
  int next = -1;

  while ((next = reader.read()) != -1) {
    //fos.write(next);
    s=s+next;
  }
  // Sucessful finished
  result = Activity.RESULT_OK;
user1909897
  • 61
  • 4
  • 10

1 Answers1

0

As you have append your inputStream from urlPath to "s"

You can access your "s" from your MainActivity using::

String data=new DownloadService().s;

just instantiate IntentService and access it's parameter with the help of period on the instantiated object.

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96