【发布时间】:2014-06-08 23:42:36
【问题描述】:
我在 android 中实现了一个登录表单,建议我应该使用可运行线程从 web 服务获取数据以进行登录身份验证,因为 android 建议使用可运行线程进行网络功能。我做了同样的事情,但是在执行线程时是否会调用任何函数,如果登录已通过身份验证,我希望该线程应该返回值,例如 1 并且在其基础上我将执行一些函数来执行下一个任务。我见过很多解决方案,比如实现类和传递参数,但没有一个能满足我的要求。我正在写下步骤以进一步解释我的问题。
1- 我想将两个参数传递给可运行线程 LoginValidation
功能。
2-我想从线程中获取返回值
函数,在我的主线程中。
3-我想调用我的父线程(主线程)的一些其他功能
在我的子线程完成后,我想在其中执行下一个任务,
就像打开个人资料屏幕一样。
我在代码中使用了全局变量“loginResponse”,但没有帮助。
我在这里写我的代码,我是android的新手,请原谅。
try{
new Thread(new Runnable() {
public void run() {
Looper.prepare();
WebserviceCall wb = new WebserviceCall();
loginResponse = wb.LoginWarden("LoginWarden");
}
}).start();
//just for testing
Toast.makeText(getApplicationContext(), "msg:"+loginResponse+"", Toast.LENGTH_SHORT).show();
if (loginResponse=="1"){
Intent myIntent = new Intent(getBaseContext(),ViolationReporter.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
finish();
}else{
Toast.makeText(getApplicationContext(), "Username or password is not valid.", Toast.LENGTH_SHORT).show();
}
}catch(Exception ex){
Toast.makeText(getApplicationContext(), "Error: "+ex.getMessage(), Toast.LENGTH_SHORT).show();
}
【问题讨论】:
-
所以你不能使用异步任务???
-
这种方式怎么用?
-
所以你可以使用 asynctask 来解决这个问题??
-
通常情况下,我会说在带有线程(或 Runnable)的 Executor 上使用 FutureTask,但Rod 是对的。 AsyncTask 是您正在寻找的。如果您想要 Activity 的结果 - 使用 developer.android.com/reference/android/app/…, int)
-
非常感谢,请发帖作为答案,我会选择@Rod_Algonquin
标签: android multithreading android-layout android-intent