如何开始在一个活动上的异步任务的工作,并告知其他活动在工作完成后OnBackground [英] How to start working on async task in one activity and inform another activity on completion of work in OnBackground

查看:114
本文介绍了如何开始在一个活动上的异步任务的工作,并告知其他活动在工作完成后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 ,但我不知道该怎么告诉其他任何活动比约其父活动其work.Please帮助me.Thanks提前整理。

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的仍然是running.When中的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.

推荐答案


  • 在第二个活动创建广播接收器,并使用其注册
    registerReceiver()方法。

  • 在AsyncTask的onPostExecute(
  • sendBroadcast())的第一种方法
    活动。

  • 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天全站免登陆