How to auto connect to server?

Viewed 24

I make a android application connect to server by using "com.mysql.jdbc.Driver". I make a class for creating a connection as a AsyncTask.

public class MySqlConnection extends AsyncTask<Integer, Integer, Connection>{
      protected void onPreExecute() {
        //
      }

      protected Connection doInBackground(Integer... integers) {
           Connection con = null;      
           Class.forName("com.mysql.jdbc.Driver");            
           con = DriverManager.getConnection("jdbc:mysql://"+ip+"/"+db_name+"?user="+username+"&password="+password);
           return con;
      }

      protected void onPostExecute(Connection connection) {
         delegate.setConnection(connection);
      }
}

This class is called in OnCreate().

  protected void onCreate(Bundle savedInstanceState) {
       MySqlConnection sqlConnection = new MySqlConnection(MainActivity.this,utils.getConnection(),ip, dbname, user, password, new ConnectionResult() {
            @Override
        public void setConnection(Connection con) {
           //set to static connection.
        }
       }
    sqlConnection.execute(1);
  }
  

It can work if a device can connect to server as initial. But if I try to turn off Wifi, it cannot to re-connect by itself. Client must restart the application for make it call OnCreate() again.

How to make it re-connection to server all the time?

0 Answers
Related