[英]How to design a Custom Generic Thread class (send and recieve data) in Android
I want to make a Custom Generic Thread Class for heavy network operations. 我想为繁重的网络操作创建自定义通用线程类。
I know how to use AsyncTask
, been using it, but now it sometimes crashes, since AsyncTask
is not recommended for operations which take more than 5 seconds.. And for implementing OOP
principles (re-usability) I decided to make a separate common Thread class which all my Transaction Screens can use to send and receive data from the network ! 我知道如何使用AsyncTask
,一直在使用它,但是现在它有时会崩溃,因为不建议将AsyncTask
用于耗时超过5秒钟的操作。为了实现OOP
原则(可重用性),我决定创建一个单独的公共线程我所有的事务屏幕都可以用来从网络发送和接收数据的类! I don't want to make a separate thread for each screen.. I searched the internet and the stackoverflow for all possible solutions... 我不想为每个屏幕创建单独的线程。.我在互联网和stackoverflow上搜索了所有可能的解决方案...
This is what I've tried so far... 到目前为止,这是我尝试过的...
import mainMenu.AccountBean;
import parser.ParserMain;
import parser.ValuesBean;
public class NetworkThread implements Runnable {
private String proccessCode;
private AccountBean ab;
private ValuesBean vb;
private String[] sd;
public NetworkThread(String proccessCode, AccountBean ab, String[]sd){
this.proccessCode = proccessCode;
this.ab = ab;
this.sd = sd;
}
@Override
public void run() {
ParserMain pm = new ParserMain();
try {
// this method does socket based transactions..
vb = pm.commenceTransaction(proccessCode, ab, sd); // I want this object 'vb' sent back to my activity..
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
My SomeActivity's Button Listener 我的SomeActivity的按钮监听器
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
NetworkThread nt = new NetworkThread("1234",ab, someOtherValuesHere);
Handler h = new Handler();
h.post(nt); // <= how to get value from here
}
});
My Questions are how to get value from this Handler
?? 我的问题是如何从此Handler
获取价值? I've seen many examples on Message
but in my scenario where do I implement Message
in Activity or the Thread
class ? 我已经看到了许多关于Message
示例,但是在我的场景中,我该在Activity或Thread
类中实现Message
吗? If there is another approach preferred I'm open to suggestions, Thanks. 如果有其他首选的方法,欢迎提出建议,谢谢。
You aren't running this in a separate Thread
, despite the fact that you've named it NetworkThread
, it isn't a Thread
This is running on the main (UI) thread, which isn't what you want. 尽管您已将其命名为NetworkThread
,但您并未在单独的Thread
运行此Thread
,它不是一个Thread
,而是在主(UI)线程上运行,这不是您想要的。
Change NetworkThread
so that it extends Thread
, not Runnable
. 更改NetworkThread
,使其扩展Thread
,而不是Runnable
。 Then do this: 然后执行以下操作:
@Override
public void onClick(View v) {
NetworkThread nt = new NetworkThread("1234",ab, someOtherValuesHere);
nt.start();
}
To get data from NetworkThread
back to the calling Activity, you could do something like this: 要将数据从NetworkThread
返回到调用方Activity,可以执行以下操作:
Declare a callback interface in NetworkThread
: 在NetworkThread
声明一个回调接口:
public Interface NetworkThreadCallback {
void reportData(ValuesBean vb);
}
and declare a member variable in NetworkThread
: 并在NetworkThread
声明一个成员变量:
private NetworkThreadCallback callback;
Then, in NetworkThread
constructor, add a parameter NetworkThreadCallback callback
and store that in the member variable callback
. 然后,在NetworkThread
构造函数中,添加一个参数NetworkThreadCallback callback
并将其存储在成员变量callback
。 After you've successfully called commenceTransaction
, do this: 成功调用commenceTransaction
,请执行以下操作:
callback.reportData(vb);
Each calling Activity
needs to implement NetworkThreadCallback
and have a method 每个调用Activity
需要implement NetworkThreadCallback
并具有一个方法
public void reportData(ValuesBean vb) {
// Do something with the ValuesBean here...
}
You just need to understand that this technique is not as robust as using a Service
. 您只需要了解这种技术并不像使用Service
那样健壮。 If your process goes to the background (User presses HOME button, takes a phone call, etc.) Android can kill your process at any time. 如果您的进程进入后台(用户按下HOME按钮,接听电话等),Android可以随时终止您的进程。 In this case your NetworkThread
will just go away. 在这种情况下,您的NetworkThread
将消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.