android - 你如何在 android 中调用 web 服务,即使 Activity 在后台,它也会继续等待响应?

标签 android web-services service android-activity android-asynctask

如果我需要连接到网络服务并确保即使在屏幕旋转或突然弹出电话或 Activity 已置于后台后它仍会继续下载,我必须采取什么方法?

我从这里的一些答案中了解到,要么使用 AsyncTask 要么使用 Service 连接到 Web 服务。连接到 Web 服务时,这真的是正确的方法吗?比较推荐的做法和常用的做法是什么?如果您还可以给我一些关于您建议的方法的解释和示例代码,我将不胜感激。

最佳答案

这真的取决于您的要求:

服务
如果您的请求需要很长时间才能完成并且您的应用程序在很大程度上取决于结果(持久数据)我建议使用服务因为android系统可以决定终止被推送到后台的应用程序并且您的请求将就这么走了。

异步任务
对于您的应用程序只需要当前状态的数据的小请求(在实际应用程序关闭后可以丢弃的数据),我会使用 AsyncTask。当方向发生变化时,这可能会变得非常棘手,因此我提供了一个示例(注意 - 它是获得想法的蓝图,而不是最终解决方案)。

我的应用程序和我的 AsyncTask 何时被终止?
当您将应用程序推送到后台时,它仍将处于 Activity 状态。如果您使用其他应用程序并且这会请求更多内存,Android 系统会决定终止您的 Activity 应用程序以释放所需的资源。从这一刻起,您的 Activity 和任务就消失了。但任务仍有机会完成并将其结果保存在数据库或 SD 卡中。

public class YourActivity extends Activity {

    private MyReqeustTask mRequestTask;

    public void onCreate(Bundle state) {
         // your activity setup

         // get your running task after orientation change
         mRequestTask = (MyRequestTask) getLastNonConfigurationInstance();

         if (mRequestTask != null) {
             mRequestTask.mActivity = new WeakReference<MyActivity>(this);
             processRequest();
         }
    }

    // call that somewhere - decide what implementation pattern you choose
    // if you need more than a single request - be inventive ;-)
    public startRequest() {
        mRequestTask = new MyRequestTask();
        mRequestTask.mActivity = new WeakReference<MyActivity>(this);
        mRequestTaks.execute(your, params);
    }

    // this is a request to process the result form your actual request
    // if it's still running, nothing happens
    public processResult() {
        if (mRequestTask == null || mRequest.getStatus() != Status.FINISHED) {
            return;
        }

        Result result = mRequest.getResult();
        // update and set whatever you want here
        mRequestTask = null;
    }

    public Object onRetainNonConfigurationInstance() {
        // retain task to get it back after orientation change
        return mRequestTask;
    }

    private static MyRequestTaks extends AsyncTask<Params, Progress, Result> {

        WeakReference<MyActivity> mActivity;

        protected Result doInBackground(Params... params) {
            // do your request here
            // don't ever youe mActivity in here, publish updates instead!
            // return your final result at the end
        }

        protected void onProgressUpdate(Progress... progress) {
            MyActivity activity = mActivity.get();

            if (activity != null) {
                // do whatever your want to inform activity about undates
            }
        }

        protected void onPostExecute(Result result) {
            MyActivity activity = mActivity.get();

            if (activity != null) {
                // this will update your activity even if it is paused
                activity.processRequest();
            }
        }
    }
}

关于android - 你如何在 android 中调用 web 服务,即使 Activity 在后台,它也会继续等待响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7549996/

相关文章:

android - 在 BroadcastReceiver 中获取 WakeLock 并在 Service 中释放它的正确模式

Android 和 OpenGL - 通过另一个对象查看对象

Android放弃音频焦点

javascript - Ajax 脚本管理器命名空间

python - 从我的 sql 检索数据并使用 python 将其发送到 Web 服务

java - 如何将 web 服务的 xml 响应转换为所需的 xml 格式?

android - gradle 3.2.1 的数据绑定(bind)问题

android - 如何对像whatsapp这样的android通知进行分组?因为 setGroup 不适合我

android - 警报管理器或服务

java - Android - 另一个线程中的 Activity 和 Service 之间的通信