【问题标题】:asyncTask from one activity running in another activity android来自在另一个活动中运行的一个活动的 asyncTask android
【发布时间】:2014-07-15 19:55:25
【问题描述】:

我只是想知道什么是最好的,可能是最简单的方法。我有两个活动 LoginAcitivty 和 Main Activity。

我在 MainActivity 中编写了一个 AsyncTask 作为内部类,它将更新发送到 Web 服务。当我单击 MainActivity 的注销按钮时,这会将应用程序返回到登录活动。即使有不同的活动正在运行,是否仍然可以运行 ASyncTask,或者是否有其他方法可以执行此类操作?

任何建议将不胜感激 谢谢

【问题讨论】:

    标签: android android-asynctask


    【解决方案1】:

    Asynctask 与创建它的“实体”相关联,在您的情况下,它将是 MainActivity,因此它不会在您的活动被破坏后继续存在(我相信您调用一次主要活动的 finis() 方法用户注销) 您可以做的是使用在后台运行的服务并使用异步任务来轮询您的服务器:

    服务应如下所示:

     public class PollService extends Service {
    
        @Override
        public void onCreate() {
          super.onCreate();    
        }
    
        public void onStart(Intent intent, int startId) {
          (new PollAsyncTask(this)).execute();
        }
    
        //callback used to retrieve the result from the asynctask
        void callBack(String result) {
          //here is your logic, taking the result back from the async task
          //eventually re-run the asynctask
          (new PollAsyncTask(this)).execute();
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    }
    

    AsyncTask 应如下所示:

     private class PollAsyncTask extends AsyncTask<String, Void, String> {
        private PollService caller;
        PollAsyncTask(PollService caller) {
             this.caller = caller;
        } 
    
        @Override
        protected String doInBackground(String... params) {
            //do your polling here and return something meaningful to the service,
            return SOMETHING_REFERRING_TO_THE_1_OF_3;
        }
    
        @Override
        protected void onPostExecute(String result) {
            //Give the result back to the caller:
            this.caller.callBack(result);
        }
    
        @Override
        protected void onPreExecute() {//nothing special here}
    
        @Override
        protected void onProgressUpdate(Void... values) {//nothing special here}
    
      }
    

    这样,您的异步任务将轮询您的服务器当前处于前台的任何活动。 该服务应在第一次运行时由您的第一个 Activity 启动(即在 onCreate 方法中):

     @Override
     public void onCreate(Bundle savedInstanceState) {
         if (savedInstanceState==null) {//only the first time
              Intent serviceIntent = new Intent();
              serviceIntent.setAction("com.yourcompany....PollService");
              startService(serviceIntent);
         }
    
     }
    

    希望这会有所帮助。

    【讨论】:

    • 我已经有一项服务可以获取 MainActivity 的 GPS 位置,如果我拥有该服务和另一个仅用于运行轮询的服务,这仍然是一个不错的选择吗?
    • 恕我直言,是的,这是一个选项。
    【解决方案2】:

    据我了解,您的 MainActivity 中有一个内部类。 因此,只需将您的 AsyncTask 放在一个单独的类中,您就可以从两个 Activity 中调用它。

    点赞:new YourAsyncTask().execute();

    您好。

    【讨论】:

    • 是的,它在一个内部类中,因为有几个 UI 元素在 Async 中运行。想知道是否有一种方法可以从内部类执行,因为我需要重新考虑 MainActivity 中的异步使用的 UI 线程?
    • 我建议您重新设计如何处理这些 UI 元素。让它简单。仅将 AsyncTask 用于必须在另一个线程中完成的工作。然后在您的 Activity 中创建一个公共方法来执行 UI 操作。现在您可以从两个活动中调用 AsyncTask。将活动的引用传入构造函数并将其保存在全局变量private Activity activity 中。在onPostExecute() 做:if(activity instanceof YourActivityThatDealsWithUI) activity.yourMethodForUI();
    • 我按照如下所示的方式进行了设置,这使我可以重新调整我的代码以从 AysncTask 中删除 UI 元素并返回到 Activity 中。它现在按我想要的方式工作!感谢您的意见:)
    • 哦,太好了,很高兴你得到了它 =)
    猜你喜欢
    • 2018-06-05
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    相关资源
    最近更新 更多