Java 如何从线程获取字符串或从线程返回字符串?

Java 如何从线程获取字符串或从线程返回字符串?,java,android,java-threads,Java,Android,Java Threads,我使用一个Web服务,它获取数据并存储在字符串中。我需要使用这个字符串,但我不能接受。全局变量在线程中不起作用。我使用的是一个传统的线程新线程(){public void run(){ 使用从主线程创建的处理程序。然后通过它传递数据 在线程中使用活动的弱引用;这样可以直接调用主线程- 使用从主线程创建的处理程序。然后通过它传递数据 在线程中使用活动的弱引用;这样可以直接调用主线程- 使用从主线程创建的处理程序。然后通过它传递数据 在线程中使用活动的弱引用;这样可以直接调用主线程- 使

我使用一个Web服务,它获取数据并存储在
字符串中。我需要使用这个
字符串
,但我不能接受。全局变量在线程中不起作用。我使用的是一个传统的线程
新线程(){public void run(){

  • 使用从主线程创建的处理程序。然后通过它传递数据
  • 在线程中使用活动的弱引用;这样可以直接调用主线程-

      • 使用从主线程创建的处理程序。然后通过它传递数据
      • 在线程中使用活动的弱引用;这样可以直接调用主线程-

          • 使用从主线程创建的处理程序。然后通过它传递数据
          • 在线程中使用活动的弱引用;这样可以直接调用主线程-

              • 使用从主线程创建的处理程序。然后通过它传递数据
              • 在线程中使用活动的弱引用;这样可以直接调用主线程-

              异步任务示例:

              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);
                   }
              
              }
              
              公共类任务扩展了异步任务{
              //你知道如何使用泛型类型吗?
              受保护字符串doInBackground(Params[]Params){
              //此代码将在单独的线程中运行
              字符串结果字符串;
              返回结果字符串;
              }
              受保护的void onPostExecute(字符串resultString){
              //此代码将调用主线程(UI线程),在此线程中,您可以更新UI,例如textView.setText(resultString);
              }
              }
              
              异步任务示例:

              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);
                   }
              
              }
              
              公共类任务扩展了异步任务{
              //你知道如何使用泛型类型吗?
              受保护字符串doInBackground(Params[]Params){
              //此代码将在单独的线程中运行
              字符串结果字符串;
              返回结果字符串;
              }
              受保护的void onPostExecute(字符串resultString){
              //此代码将调用主线程(UI线程),在此线程中,您可以更新UI,例如textView.setText(resultString);
              }
              }
              
              异步任务示例:

              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);
                   }
              
              }
              
              公共类任务扩展了异步任务{
              //你知道如何使用泛型类型吗?
              受保护字符串doInBackground(Params[]Params){
              //此代码将在单独的线程中运行
              字符串结果字符串;
              返回结果字符串;
              }
              受保护的void onPostExecute(字符串resultString){
              //此代码将调用主线程(UI线程),在此线程中,您可以更新UI,例如textView.setText(resultString);
              }
              }
              
              异步任务示例:

              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);
                   }
              
              }
              
              公共类任务扩展了异步任务{
              //你知道如何使用泛型类型吗?
              受保护字符串doInBackground(Params[]Params){
              //此代码将在单独的线程中运行
              字符串结果字符串;
              返回结果字符串;
              }
              受保护的void onPostExecute(字符串resultString){
              //此代码将调用主线程(UI线程),在此线程中,您可以更新UI,例如textView.setText(resultString);
              }
              }
              
              使用LocalBroadcastManager发送和接收数据。 这样可以避免内存泄漏问题

              下面是活动的代码

              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 
              
                      }
                  };
              
              }
              
              这里是下载字符串的线程的代码

              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);
              
                  }
              
              }
              

              您可以使用线程而不是IntentService。

              使用LocalBroadcastManager发送和接收数据。 这样可以避免内存泄漏问题

              下面是活动的代码

              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 
              
                      }
                  };
              
              }
              
              这里是下载字符串的线程的代码

              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);
              
                  }
              
              }
              

              您可以使用线程而不是IntentService。

              使用LocalBroadcastManager发送和接收数据。 这样可以避免内存泄漏问题

              下面是活动的代码

              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 
              
                      }
                  };
              
              }
              
              这里是下载字符串的线程的代码

              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);
              
                  }
              
              }
              

              您可以使用线程而不是IntentService。

              使用LocalBroadcastManager发送和接收数据。 这样可以避免内存泄漏问题

              下面是活动的代码

              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 
              
                      }
                  };
              
              }
              
              这里是下载字符串的线程的代码

              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);
              
                  }
              
              }
              

              您可以使用线程而不是IntentService。

              您可以使用异步任务:

              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
                 }
              }
              
              private类扩展异步任务{
              受保护的字符串背景(无效…无效){
              //做你的Web服务处理
              返回您的_字符串;
              }
              受保护的void onPostExecute(字符串结果){
              //检索UI线程中的字符串
              }
              }
              
              您可以使用异步任务:

              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
                 }
              }
              
              private类扩展异步任务{
              受保护的字符串背景(无效…无效){
              //做你的Web服务处理
              返回您的_字符串;
              }
              受保护的void onPostExecute(字符串结果){
              //检索UI线程中的字符串
              }
              }
              
              您可以使用异步任务:

              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
                 }
              }
              
              private类扩展异步任务{
              受保护的字符串背景(无效…无效){
              //做你的Web服务处理
              返回您的_字符串;
              }
              受保护的void onPostExecute(字符串结果){
              //检索UI线程中的字符串
              }
              }
              
              您可以使用异步任务:

              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
                 }
              }
              
              private类扩展异步任务{
              受保护的字符串背景(无效…无效){
              //做你的Web服务处理
              返回您的_字符串;
              }
              受保护的void onPostExecute(字符串结果){
              //检索UI线程中的字符串
              }
              }
              
              为什么不使用AsyncTask?全局变量在线程中不起作用?当您匿名创建它时?真的吗?先生,我不这么认为,添加修改器volatile为什么不使用AsyncTask?全局变量在线程中不起作用?当您匿名创建它时?真的吗?先生,我不这样认为,添加修改器volatile为什么不不使用AsyncTask?全局变量在线程中不工作?当您匿名创建它时?真的吗?先生,我不这么认为,添加修饰符volatile为什么不使用AsyncTask?全局变量在线程中不工作