我应该使用 AsyncTask 建立 XMPP 连接吗?
Should I use AsyncTask to establish an XMPP connection?
我正在使用 Smack
连接到 Android
中的 XMPP server
。这是我的代码:
static void openConnection() {
try {
if (null == connection || !connection.isAuthenticated()) {
XMPPTCPConnectionConfiguration.Builder configuration = XMPPTCPConnectionConfiguration.builder();
configuration.setHost(SERVER_HOST);
configuration.setPort(SERVER_PORT);
configuration.setServiceName(SERVICE_NAME);
configuration.setUsernameAndPassword(new TinyDB(context.getApplicationContext()).getString("username"), new TinyDB(context.getApplicationContext()).getString("password"));
configuration.setDebuggerEnabled(true);
connection = new XMPPTCPConnection(configuration.build());
connection.setUseStreamManagement(true);
connection.setUseStreamManagementResumption(true);
ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(connection);
reconnectionManager.enableAutomaticReconnection();
reconnectionManager.setReconnectionPolicy(ReconnectionManager.ReconnectionPolicy.RANDOM_INCREASING_DELAY);
connection.connect();
connection.login();
}
} catch (XMPPException xe) {
xe.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
所以当我调用 openConnection()
时,我应该在 AsyncTask
中调用还是没有必要?我有点困惑。
是的,正如官方文档指出的那样:
AsyncTask enables proper and easy use of the UI thread. This class
allows to perform background operations and publish results on the UI
thread without having to manipulate threads and/or handlers.
When i call openConnection() should i do that in an asynctask or that is not neccesary?
很快,是。与 networking
相关的所有内容都应移至 another thread
以避免阻塞 main thread
。因此 AsyncTask
的 doInBackground()
在另一个 thread
上运行,您应该在其中调用 function
.
您应该在 Android Service
内管理您的 XMPP(TCP)Connection
。服务状态(running/stopped)应该重组连接状态:当服务为运行时,应该建立连接或者服务应该尝试建立连接(如果数据连接可用)。如果服务停止,则也断开连接。
四处搜索后,我选择不为我的 smack 项目使用 AsyncTask。
- 它的线程模型在Android版本之间有很大的不同,需要注意,而且在蜂巢之后,它是单线程,长时间阻塞会导致整个设备出现问题,也使用 AsyncTask , xmpp 和 bosh 可能导致长达 seconds/minutes
的长时间阻塞
- AsyncTask隐式引用了activity,如此长的操作会导致内存问题,或者异常处理不当容易造成内存泄漏
- 如果引用 activity 被重置,AsyncTask 的结果将丢失,但是 Android 中的 activity 也可以像简单的设备轮换或网络配置更改一样简单地重置许多保存和恢复实例以使其可用,因为每个 xmpp 操作都可能是很长的任务
我正在使用 Smack
连接到 Android
中的 XMPP server
。这是我的代码:
static void openConnection() {
try {
if (null == connection || !connection.isAuthenticated()) {
XMPPTCPConnectionConfiguration.Builder configuration = XMPPTCPConnectionConfiguration.builder();
configuration.setHost(SERVER_HOST);
configuration.setPort(SERVER_PORT);
configuration.setServiceName(SERVICE_NAME);
configuration.setUsernameAndPassword(new TinyDB(context.getApplicationContext()).getString("username"), new TinyDB(context.getApplicationContext()).getString("password"));
configuration.setDebuggerEnabled(true);
connection = new XMPPTCPConnection(configuration.build());
connection.setUseStreamManagement(true);
connection.setUseStreamManagementResumption(true);
ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(connection);
reconnectionManager.enableAutomaticReconnection();
reconnectionManager.setReconnectionPolicy(ReconnectionManager.ReconnectionPolicy.RANDOM_INCREASING_DELAY);
connection.connect();
connection.login();
}
} catch (XMPPException xe) {
xe.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
所以当我调用 openConnection()
时,我应该在 AsyncTask
中调用还是没有必要?我有点困惑。
是的,正如官方文档指出的那样:
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
When i call openConnection() should i do that in an asynctask or that is not neccesary?
很快,是。与 networking
相关的所有内容都应移至 another thread
以避免阻塞 main thread
。因此 AsyncTask
的 doInBackground()
在另一个 thread
上运行,您应该在其中调用 function
.
您应该在 Android Service
内管理您的 XMPP(TCP)Connection
。服务状态(running/stopped)应该重组连接状态:当服务为运行时,应该建立连接或者服务应该尝试建立连接(如果数据连接可用)。如果服务停止,则也断开连接。
四处搜索后,我选择不为我的 smack 项目使用 AsyncTask。
- 它的线程模型在Android版本之间有很大的不同,需要注意,而且在蜂巢之后,它是单线程,长时间阻塞会导致整个设备出现问题,也使用 AsyncTask , xmpp 和 bosh 可能导致长达 seconds/minutes 的长时间阻塞
- AsyncTask隐式引用了activity,如此长的操作会导致内存问题,或者异常处理不当容易造成内存泄漏
- 如果引用 activity 被重置,AsyncTask 的结果将丢失,但是 Android 中的 activity 也可以像简单的设备轮换或网络配置更改一样简单地重置许多保存和恢复实例以使其可用,因为每个 xmpp 操作都可能是很长的任务