我的代码中运行着这段代码onDestroy
功能:
@Override
protected void onDestroy() {
if (!(null == theUser.glideId)) {
JSONObject req = new JSONObject();
try {
req.put("actionKey", "UserPresenceInactive");
req.put("userId", theUser.userId);
new ServerRequest().execute(req); //Run an AsyncTask
} catch (JSONException e) {
e.printStackTrace();
}
}
super.onDestroy();
}
In the AsyncTask
我向服务器发送一个请求(仅返回 200 响应)。
我的问题是,这样做有什么影响(如果有的话)?
Activity 会被销毁吗?如果服务器没有响应,应用程序是否会保持唤醒状态并可能进入 ANR?有什么想法吗?
edit
我尝试这样做,但得到了android.os.NetworkOnMainThreadException
.
new Runnable() {
@Override
public void run() {
JSONObject req = new JSONObject();
try {
req.put("actionKey", "UserPresenceInactive");
req.put("userId", theUser.userId);
new ServerRequest().execute(req); //Run an AsyncTask
} catch (JSONException e) {
e.printStackTrace();
}
}
}.run();
更新#2
using a Thread
代替Runnable
成功了!
Yes the Activity
被摧毁但是AsyncTask
正在做它的工作(在后台工作)。不会有任何ANR
因为你没有在 UI 上做任何后台操作。如果您在之后更新 UI 上的任何视图AsyncTask
完成后会有一个NULLPOINTER Exception
AFAIK。但是,这不是一个运行 AsyncTask 的好主意onDestroy()
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)