是否可以在不同的类中使用连接类(AsyncTask)?

例如,我的MainActivity中包含以下代码,但是我想在“不同活动”中使用它,如何将其用作辅助线程?:

public class Connect extends AsyncTask<String, Void, String> {

    String resultadoConexion;

    @Override
    protected String doInBackground(String... urls) {

        Connection conn;
                       try {

                            Class.forName("com.mysql.jdbc.Driver");
                            conn = DriverManager.getConnection("jdbc:mysql://XXXXXXXXXX?user=XXXX&password=XXXX");
                            Log.i("Exito","Conexion a la base de datos realizada con exito");

                           resultadoConexion = "Conectado";
                        }

                        catch (Exception e)
{
                            resultadoConexion = "Error al conectar";
                            Log.i("ErrorBBDD", "Error: " + e.getMessage());
                            e.printStackTrace();
                        }
        return resultadoConexion;
    }

    //Metodo que ejecuta la tarea del hilo
    public void Conectar() {
        Connect task = new Connect();
        task.execute();
    }

    //Creamos el metodo setText para poder asignar dentro del hilo secundario
    private void setText(final TextView text, final String value)
    {
        runOnUiThread(new Runnable(){
            @Override public void run(){
                text.setText(value);
            }
        });
    }


}

最佳答案

将内联类放置在单独的类文件中,然后重复使用。

ConnectAsyncTask.class

public class ConnectAsyncTask extends AsyncTask<String, Void, String> {

    String resultadoConexion;

    @Override
    protected String doInBackground(String... urls) {

        Connection conn;
                       try {

                            Class.forName("com.mysql.jdbc.Driver");
                            conn = DriverManager.getConnection("jdbc:mysql://XXXXXXXXXX?user=XXXX&password=XXXX");
                            Log.i("Exito","Conexion a la base de datos realizada con exito");

                           resultadoConexion = "Conectado";
                        }

                        catch (Exception e)
{
                            resultadoConexion = "Error al conectar";
                            Log.i("ErrorBBDD", "Error: " + e.getMessage());
                            e.printStackTrace();
                        }
        return resultadoConexion;
    }

    //Metodo que ejecuta la tarea del hilo
    public void Conectar() {
        Connect task = new Connect();
        task.execute();
    }

    //Creamos el metodo setText para poder asignar dentro del hilo secundario
    private void setText(final TextView text, final String value)
    {
        runOnUiThread(new Runnable(){
            @Override public void run(){
                text.setText(value);
            }
        });
    }


然后在您想要的任何地方使用它:

MyActivity1:

import package...ConnectAsyncTask;
public void onCreate(Bundle savedinstance) {
  new ConnectAsyncTask().execute(url);
}


MyActivity2:

import package...ConnectAsyncTask;
public void onCreate(Bundle savedinstance) {
  new ConnectAsyncTask().execute(url);
}


或者直接重用您的活动的内部类。例如,您的活动名称是MyActivity,那么您可以像下面这样获得connect任务的实例:

new MyActivity.Connect()

关于android - AsyncTask类在differents Activity 中吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29344104/

10-12 04:18