Alert dialog freeze my app

2019-09-13 14:57发布

问题:

Here there is the code that check the internet connection and then if the isconnected is false show a message in a allert dialog. The problem is that if isconnected is true and (i tried to put if(isconnected) clause instead if(!isconnected)) every works. But if i put off every network on my phone when VM execute show() every freeze. Why? Thanks to all:

final AlertDialog.Builder dialog= new AlertDialog.Builder(this);


    ((Button)findViewById(R.id.listabutton)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AsyncTask asyncTask= new AsyncTask() {
                boolean isconnected=true;
                @Override
                protected Object doInBackground(Object[] params) {


                        ConnectivityManager conMgr = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

                        NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
                        isconnected = activeNetwork != null &&
                                activeNetwork.isConnectedOrConnecting();
                    Log.i("StateNet",isconnected+"");


                    return null;
                }

                @Override
                protected void onPostExecute(Object o) {
                    if(!isconnected){

                        dialog.setMessage("Controlla la tua conessione a internet")
                                .setTitle("Ops problemino con internet")
                                .setPositiveButton("Ok", ok)
                                .show();

                    }

                    super.onPostExecute(o);
                }

            };

回答1:

You do not need AsyncTask to check the internet connection, so please keep it simple.

I think that your onClick method should looks like this:

@Override
public void onClick(View v) {
      if (isConnectionAvailable(context)) {
          // connected
      } else {
          // not connected
      }
};

public static boolean isConnectionAvailable(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}

Do not forget the ACCESS_NETWORK_STATE and INTERNET permissions in your Manifest.



回答2:

Try with this...

((Button)findViewById(R.id.listabutton)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        if(isDeviceOnline(context)){
          //do something.
        }else{
          AlertDialog.Builder dialog= new AlertDialog.Builder(context);
          dialog.setMessage("Not connected");
          dialog.setTitle("Error!");
          dialog.setPositiveButton(.....);
          dialog.create();
          dialog.show();
        } 

    }
}


public boolean isDeviceOnline(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}

And yes, add ACCESS_NETWORK_STATE and INTERNET permissions in the manifest file