本文介绍了Android:AsyncTask 的替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的应用程序,我为对外部 API 的所有网络调用创建了一个框架.我将所有内容都放在服务"中,例如:UserServiceMessageService 等.所以现在当我想调用网络方法时,我无法在的 UI 线程中执行此操作我的 Activity 所以我做了一个 AsyncTask 一切都很好 =)

for my app I made a framework for all network calls to an external API. I put everything in "Services" like: UserService, MessageService etc. So now when I want to call a network method I can't do this in the UI-Thread of my Activity so I made an AsyncTask and everything was just fine =)

但现在我想要一些东西,比如将要执行的方法提供给 AsyncTask,所以我不需要为我的网络框架的每个方法编写一个 AsyncTask.所以,而不是像这样:

But now I want something like giving the method to execute to the AsyncTask so I don't need to write an AsyncTask for every method of my network-framework. So instead of having something like this:

public class NetworkTask extends AsyncTask<UserService, Void, Void> {

    @Override
    protected Void doInBackground(UserService... params) {
        UserService userService = params[0];
        userService.getUser();
        return null;
    }
}

我想要更抽象"的东西,所以 AsyncTask 不需要知道getUser"方法?但我找不到任何关于如何执行此操作的想法...也许 AsyncTask 对此并不好?

I want something more "abstract" so the AsyncTask doesn't need to know the "getUser" method? But I can't find any idea of how to do this... Maybe the AsyncTask is not good for this anyway?

推荐答案

AsyncTask 有很多替代方案:

There are plenty AsyncTask alternatives :

https://android-arsenal.com/tag/9

and plus Needle - Android 多线程库

and plus Needle - Multithreading library for Android

http://zsoltsafrany.github.io/needle/

这篇关于Android:AsyncTask 的替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 05:49