简体   繁体   English

在 Android 中进行网络调用以响应 UI 事件

[英]Making network calls in Android in response to UI event

I am building an Android application that needs to make certain network calls in response to UI events.我正在构建一个 Android 应用程序,它需要进行某些网络调用以响应 UI 事件。 I am struggling to find an elegant way to send out those calls from a UI callback, though.不过,我正在努力寻找一种优雅的方式来从 UI 回调中发出这些调用。 An AsyncTask isn't really a good fit because all I want to do is just send out the call - any responses responses will be handled by callbacks in my receiver code. AsyncTask 并不是很合适,因为我想做的只是发出调用 - 任何响应都将由我的接收器代码中的回调处理。 Likewise, spawning a new thread just to perform a single network call seems wasteful and bad practice, especially considering that in certain cases there will be many, many calls per second as the user interacts with the UI.同样,生成一个新线程只是为了执行单个网络调用似乎是浪费和不好的做法,特别是考虑到在某些情况下,当用户与 UI 交互时,每秒会有很多很多调用。

Note: by "network call" what I really mean is sending out some bytes through a UDP DatagramSocket注意:通过“网络调用”我真正的意思是通过 UDP DatagramSocket 发送一些字节

I thought about having a network TX thread with a "send queue" looping continuously that the UI thread could post messages to, but that would seem an incredible waste of CPU cycles during times when there may be several seconds between when calls need to be made.我想过让网络 TX 线程具有“发送队列”连续循环,UI 线程可以向其发送消息,但是在需要进行呼叫之间可能有几秒钟的时间期间,这似乎是对 CPU 周期的难以置信的浪费.

I have a feeling that the elegant solution is a combination of the above "looping TX thread" idea, but with lock objects so it doesn't loop unless there's new data to send - but I'm not very experienced with concurrency, locks, and the like, so I'm hoping somebody here can point me in the right direction.我有一种感觉,优雅的解决方案是上述“循环 TX 线程”想法的组合,但使用锁对象,因此除非有新数据要发送,否则它不会循环 - 但我对并发、锁的经验不是很丰富,等等,所以我希望这里有人能指出我正确的方向。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks in advance :)提前致谢 :)

Fort the backgroundwork you use a ThreadPoolExecutor like this. Fort 背景工作,您使用像这样的 ThreadPoolExecutor。 I use a specialised class for this.我为此使用了一个专门的类。 You post runnables to the executor.您将 runnable 发布到执行程序。

public class RemoteConnections {

//Variables and constants to manange the threadpool
private final int KEEP_ALIVE_TIME = 1;
private final TimeUnit KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;
private final int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
private ThreadPoolExecutor mThreadpool;

private RemoteRepository(Application application) {
    mApplication = application;
    //initialize the threadpool for networking
    final BlockingQueue<Runnable> mQueue = new LinkedBlockingQueue<Runnable>();
    mThreadpool = new ThreadPoolExecutor(NUMBER_OF_CORES, NUMBER_OF_CORES, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT.SECONDS, mQueue);
}


public void loadWhatEver() {

    mThreadpool.execute(new Whatever());
}


private class Whatever implements Runnable {
    public  Whatever(//setup whatever you need perhaps your callback listener) {
        mListener = listener;
    }

    @Override
    public void run() {
        //do your downloading here
        //make the callback to the listener
    }    
}

In the activity or fragment just call remote.loadWhatEver() when the event happens.在活动或片段中,只需在事件发生时调用 remote.loadWhatEver()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM