0

I wrote some code to start a service from AsyncTask, and I can see on debug mode that the service is started.

The code for starting the service on the background on AsyncTask is as follows,

protected String doInBackground(String... params) {
//starts service number activite
MyResultReceiver resultReceiver = new MyResultReceiver(null);
Intent  intent = new Intent(context, MyService.class);
intent.putExtra("receiver", resultReceiver);
context.startService(intent);

and the serivce is:

    public class MyService extends Service{


    ResultReceiver resultReceiver;


       @Override
     public int onStartCommand(Intent intent, int flags, int startId) {

       //resultReceiver = intent.getParcelableExtra("receiver");


        resultReceiver = intent.getParcelableExtra("receiver");
        Bundle data = new Bundle();
          data.putString("key",getActivityUrl() );
       resultReceiver.send(2, data);

       resultReceiver.send(2,null);
     return START_STICKY;
     }



        @Override
          public IBinder onBind(Intent intent) {
          return null;
             }

     public String getActivityUrl() {
   String str = null;   
       try{

URL url = null;
        try {
            url = new URL("http://www.google.com");
        } catch (MalformedURLException ex) {
            Logger.getLogger(MyService.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

        while ((str = in.readLine()) != null) {
            // str is one line of text; readLine() strips the newline character(s)
        }
                //all is ok

        in.close();

     }       catch (IOException ex) {
        Logger.getLogger(MyService.class.getName()).log(Level.SEVERE, null, ex);
    }finally{ stopSelf();
     }

    //resultReceiver.send(100,null);

    return str;
}

}

Another question: How can I send a String and not only an int with resultReceiver.send?

Vitaly Menchikovsky
  • 7,684
  • 17
  • 57
  • 89

1 Answers1

1

how can I send string and not only int with resultReceiver.send?

Just create Bundle and put in String.

Bundle data = new Bundle();
data.putString("key", data);
resultReceiver.send(2, someData);


Note: Only for sure, first parameter of send() method imagine resultCode for onReceiveResult method.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • why you are using Asynctask and ResultReceiver? AsyncTask has onProgressUpdate method for update your UI also from your code im little confused, dirty written. – Simon Dorociak Jun 29 '12 at 12:19
  • I want to get some data drom stream and do it with service and on while do some proccess bar on loading. see I added the full code for my service. the problem is that my service isnt starts and I dont know why – Vitaly Menchikovsky Jun 29 '12 at 12:22
  • 1
    so when you want to do it with service, dont complicate it with AsyncTask and do your work in Service and send progress via resultreceiver. you need to read some tutorial, i cant teach you basics. your application logic is not good. a recommend to you use IntentService with ResultReceiver. [here](http://stackoverflow.com/questions/11137754/comunication-between-service-web-and-activity-back-and-forth-in-android/11138045#11138045) in my another answer is snippet of code how to use IntentService with ResultReceiver. – Simon Dorociak Jun 29 '12 at 12:23
  • I will take a look but any idea why me service not starting? I edit full code of my service – Vitaly Menchikovsky Jun 29 '12 at 12:47
  • just i wrote, bad application logic. – Simon Dorociak Jun 29 '12 at 12:48
  • I wrote as you said but still the same thing this post here:http://stackoverflow.com/questions/11263138/service-not-starting-from-asynctask – Vitaly Menchikovsky Jun 29 '12 at 14:52
  • did you check link that i meant? – Simon Dorociak Jun 29 '12 at 14:55
  • yes as you can see I did like you said on you link you can see it on my link before. I am getting the same result. – Vitaly Menchikovsky Jun 29 '12 at 15:17
  • my problem with this line serviceIntent.putExtra("receiver", new tools.DownloadReceiver(new Handler())); if I remove this line I can see that on debug its create a service but when I did this line its creashed. why is that? – Vitaly Menchikovsky Jun 29 '12 at 15:31
  • putExtra("receiver", new DownloadReceiver(new Handler())); you should have resultreceiver as inner class in your Main Activity class. – Simon Dorociak Jun 29 '12 at 15:32
  • did a class on my asy task private class DownloadReceiver extends ResultReceiver{ public DownloadReceiver(Handler handler) { super(handler); } @Override protected void onReceiveResult(int resultCode, Bundle resultData) { super.onReceiveResult(resultCode, resultData); // if (resultCode == DownloadService.UPDATE_PROGRESS) { int progress = resultData.getInt("progress");}} – Vitaly Menchikovsky Jun 29 '12 at 15:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13226/discussion-between-vitaly-menchikovsky-and-hawaii-five-0) – Vitaly Menchikovsky Jun 29 '12 at 15:50