如何开始在一个活动中处理异步任务并在 OnBackground 中完成工作时通知另一个活动 [英] How to start working on async task in one activity and inform another activity on completion of work in OnBackground

查看:14
本文介绍了如何开始在一个活动中处理异步任务并在 OnBackground 中完成工作时通知另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下行为,但我不确定如何:

I would like to achieve the following behaviour, but I'm not sure how:

   1. User start an activity
   2. Activity starts an AsyncTask
   3. After initiating the AsyncTask,the control is transferred to another activity.
   4. Now,when the AsynTask finishes its work,the 2nd activity should get to know that        AsyncTask has completed its working in the previous activity.

我不知道如何实现该功能.我知道如何使用 AsyncTask 但我不知道如何通知除其父活动之外的任何活动完成其工作.请帮助我.提前致谢.

I don't know how to achieve that functionality.I know how to use AsyncTask but i don't know how to inform any activity other than its parent activity about the finishing of its work.Please help me.Thanks in advance.

我想这样做是因为在第一个活动中我想在 AsyncTask 中开始一些工作,然后控制权转移到第二个活动但 AsyncTask 仍在运行.当 AsyncTask 完成其功能时,我想通知第二个活动关于它的活动,然后想在第二个活动中执行一些功能.

I want to do it because in 1st activity i want to start some working in AsyncTask,then the control is transferred to 2nd activity but the AsyncTask is still running.When the AsyncTask will complete its functionality,i want to inform the 2nd activity about it and then want to perform some function in the 2nd activity.

推荐答案

  • 在第二个活动中创建 BroadcastReceiver 并使用注册registerReceiver() 方法.
  • sendBroadcast() 在 AsyncTask onPostExecute() 方法中的第一个活动.

    • Create BroadcastReceiver in second activity and register it using registerReceiver() method.
    • sendBroadcast() in AsyncTask onPostExecute() method of first Activity.

      public class SecondActivity extends Activity {
          private BroadcastReceiver myBroadcastReceiver =
              new BroadcastReceiver() {
                  @Override
                  public void onReceive(...) {
                      ...
                  }
             });
      
          ...
      
          public void onResume() {
              super.onResume();
              IntentFilter filter = new IntentFilter();  
                          filter.addAction("com.example.asynctaskcompleted");  
                          filter.addCategory("android.intent.category.DEFAULT");
              registerReceiver(myBroadcastReceiver, filter);
          }
      
          public void onPause() {
              super.onPause();
              ...
              unregisterReceiver(myBroadcastReceiver);
          }
          ...
      }
      
      
      public class FirstActivity extends Activity {
          private class MyTask extends AsyncTask<Void, Void, Void> {
              protected Void doInBackground(Void... args) {
              ...
          }    
      
          protected void onPostExecute(Void result) {
              Intent intent = new Intent ("com.example.asynctaskcompleted");            
      
              FirstActivity.this.sendBroadcast(intent);
       }
      

      }

      这篇关于如何开始在一个活动中处理异步任务并在 OnBackground 中完成工作时通知另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆