0

Im using a webservice that get a data and stores in a String. I need to use this String but I cant take it. Global variables don't work in Threads. I'm using a traditional Thread new Thread() { public void run() {.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
  • 1
    Why are you don't use AsyncTask? http://developer.android.com/reference/android/os/AsyncTask.html – krystian71115 Jul 05 '15 at 01:09
  • 2
    global variables do not work in threads? when you create it anonymously? really? sir, i do not think so, add the modifier volatile – Elltz Jul 05 '15 at 01:19

4 Answers4

2

Example of AsyncTask:

public class Task extends AsyncTask<Params, Progress, String> {
     // are you know how to use generic types?

     protected String doInBackground(Params[] params){
           // this code will run in seperate thread
           String resultString;
           return resultString;
     }
     protected void onPostExecute(String resultString){
           // this code will call on main thread (UI Thread) in this thread you can update UI e.g. textView.setText(resultString);
     }

}
krystian71115
  • 1,927
  • 17
  • 36
1

Use LocalBroadcastManager to send and receive data. This way you can avoid memory leak issues.

Here is code for activity

public class YourActivity extends Activity {

    private void signal(){
        LocalBroadcastManager.getInstance(YourActivity.this).registerReceiver(receiver, new IntentFilter("Your action name"));
        Intent yourAction = new Intent(YourActivity.this, YourIntentService.class);
        String string = "someData";
        yourAction .putExtra("KEY_WITH_URL", string);
        startService(yourAction);
    }

    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String string = intent.getStringExtra("KEY_WITH_ANSWER");
            //Do your code 

        }
    };

}

Here code for thread which download String or whatever

public class YourIntentService extends IntentService {

    @Override
    protected void onHandleIntent(Intent intent) {
        // Download here

        Intent complete = new Intent ("Your action name");
        complete.putExtra("KEY_WITH_ANSWER", stringToReturn);
        LocalBroadcastManager.getInstance(YourIntentService.this).sendBroadcast(complete);

    }

}

You can use Thread instead of IntentService.

Aleksandr
  • 787
  • 6
  • 22
0
  • use a Handler created from the main thread. Then pass your data throuh it
  • use a weak reference of your activity in your thread; this way you can call directly the main thread - Activity.runOnUiThread(Runnable)

    ...
    Activity activity = activityWeakReference.get();
    if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run()
            {
                // you are in main thread, pass your data
            }
        });
    }
    
krystian71115
  • 1,927
  • 17
  • 36
Dimitar Genov
  • 2,056
  • 13
  • 11
0

You can use Async task:

private class Whatever extends AsyncTask<Void, Void, String> {
   protected String doInBackground(Void... void) {
       // do your webservice processing
       return your_string;
   }

   protected void onPostExecute(String result) {
       // Retrieves the string in the UI thread
   }
}
krystian71115
  • 1,927
  • 17
  • 36
E. Fernandes
  • 3,889
  • 4
  • 30
  • 48