【问题标题】:using method inside asyntask in android在android中使用asynctask中的方法
【发布时间】:2013-08-10 06:59:20
【问题描述】:

我创建了一个简单的登录应用程序,它从 sqlserver 获取用户名和密码......它工作正常......

我想在登录过程中使用 asyntask 显示一个进度条... 但我不知道在 asyntask 中使用参数...如果有人告诉我如何将我的方法放在 asyntask 的 doInbackground 中以及我应该使用什么参数....

我的代码是;.....

public void save(){
    initilize();
    ResultSet rs = null;
    String mylog=id.getText().toString();
    String mypass=pass.getText().toString();
    try{
    Statement statement=connect.createStatement();
    rs=statement.executeQuery("LOGIN '"+mylog+"', '"+mypass+"'");
    }catch(Exception e){
        e.printStackTrace();
    }

    if(mylog.equals("")||mypass.equals("")){

        Toast.makeText(getApplicationContext(), "empty fields", Toast.LENGTH_SHORT).show();

    } else
        try {
            if(rs.next()){

                Intent i=new Intent(getApplicationContext(),Act2.class);
                startActivity(i);
            }
            else if(rs.next()==false){

                Toast.makeText(getApplicationContext(), "incorrect login", Toast.LENGTH_SHORT).show();
                id.setText("");
                pass.setText("");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

如果可以将相同的方法 save() 保存在 asyntask 的 doInbackground() 中...

【问题讨论】:

    标签: android android-asynctask progress-bar


    【解决方案1】:

    进行快速重构(请注意,就目前而言,这是非常糟糕的做法和编码,您必须重构此代码以使其更易于维护并避免重复):

    public class MyAsyncTask extends AsyncTask<> {
    

    私人活动活动; 布尔结果; 私人字符串 myLog; 私人字符串 myPass; 私有连接连接;

    public MyAsyncTask(Activity activity, Connection connect) {
        this.activity = activity;
        this.connect = connect;
    }
    
    @Override
    protected void onPreExecute() {
        //show your progress dialog
    }
    
    @Override
    protected Object doInBackground(Object[] objects) {
    
        ResultSet rs = null;
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    initilize();
                    mylog=id.getText().toString();
                    mypass=pass.getText().toString();
                }
            });
    
            try{
                Statement statement=connect.createStatement();
                rs=statement.executeQuery("LOGIN '"+mylog+"', '"+mypass+"'");
            }catch(Exception e){
                e.printStackTrace();
            }
    
            if(mylog.equals("")||mypass.equals("")){
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity.getApplicationContext(), "empty fields", Toast.LENGTH_SHORT).show();
                    }
                });
    
    
            } else
                try {
                    if(rs.next()){
                    result = true;
    
                    }
                    else if(rs.next()==false){
    
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(activity.getApplicationContext(), "incorrect login", Toast.LENGTH_SHORT).show();
                                id.setText("");
                                pass.setText("");
                            }
                        });
    
                    }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
        return null;
    }
    
    @Override
    protected void onPostExecute(Object o) {
        //hide your progress dialog
        if(result == Boolean.TRUE){
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Intent i=new Intent(activity.getApplicationContext(),Act2.class);
                    activity.startActivity(i);
                }
            });
    
        }
    
    }
    }
    

    然后在您的 Activity 中执行以下操作:

    MyAsyncTask a = new MyAsyncTask(this, connect); //im guessing "connect" is your Connection object
    a.execute();
    

    正如我所说,我进行了快速重构以使代码能够正常工作,但此处不考虑最佳实践和良好实现。

    【讨论】:

    • 我应该将什么参数传递给 asyntask,?,?>
    • 您不需要将参数传递给 AsyncTask,?,?> 的代码只是在构造函数上传递参数,我猜这些是您需要传递的参数(活动用于访问 UI 线程和 Connection 对象)
    【解决方案2】:

    也许,您可以使用计时器来检查您的 登录已准备就绪。只要不是,你就显示你的进度条。如果它准备好了,你可以关闭酒吧并开始一个新的活动或任何事情。计时器运行可以在 u UI 线程上运行。 问候:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多