Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android主线程中正确运行代码_Android - Fatal编程技术网

在android主线程中正确运行代码

在android主线程中正确运行代码,android,Android,于是开始编写android应用程序,主要是开源和学习该语言的教程 我现在有一个问题,如果用户登录过一次,他就再也不用登录了。在阅读了这里的许多答案后,我使用了SharedPref,但由于这个原因,应用程序正在跳帧 这是我的密码 SharedPreferences prefs = getPreferences(Login.this); prefs = PreferenceManager.getDefaultSharedPreferences(Login.this); String remUsern

于是开始编写android应用程序,主要是开源和学习该语言的教程

我现在有一个问题,如果用户登录过一次,他就再也不用登录了。在阅读了这里的许多答案后,我使用了SharedPref,但由于这个原因,应用程序正在跳帧

这是我的密码

SharedPreferences prefs = getPreferences(Login.this);
prefs = PreferenceManager.getDefaultSharedPreferences(Login.this);
String remUsername = prefs.getString("username", null);
String remPassword = prefs.getString("password", null);

if (remUsername != null && remPassword != null)
{
    String result = null;

    try {
        result = imService.authenticateUser(
        remUsername.toString(),
        remPassword.toString());
    } catch (UnsupportedEncodingException e) {
        //TODO Auto-generated catch block
        e.printStackTrace();
    }

    Intent i = new Intent(Login.this, FriendList.class);
    startActivity(i);
    Login.this.finish();
}

跳转帧可能是由主线程执行的资源密集型操作(如HTTP请求)造成的。您必须在单独的线程中执行此类操作,同时只为主线程保留UI和非密集型操作(这就是为什么它被称为“UI”线程)


阅读基础知识。然后,您可以使用实现它,例如。但是,为了简单起见,建议使用一个专门的库,例如or。

我想您的
authenticateUser()
方法执行HTTP请求?如果是这样,您必须在单独的线程中执行,否则您将面临跳帧甚至ANR。阅读:是的,它执行HTTP请求。我已经读到,它不应该在主线程中,但我不知道在哪里做它,如果我把它放在任何其他地方的使用没有得到认证,需要重新登录