0

recently I have WI-FI connecter code . and I add if wi-fi connect button click, showing dialog. but showing dialog continued remains. I want when WI-FI connecting showing dialog, and completed wifi connect ,dismiss dialog.

I try this. but is I don't know error logcat.

How programatically showing progress dialog when wifi connecting ?

@Override
    public void onClick(View v) {


        final WifiConfiguration config = Wifi.getWifiConfiguration(mWifiManager, mScanResult, mScanResultSecurity);
        boolean connResult = false;

        if (config != null) {
            connResult = Wifi.connectToConfiguredNetwork(mFloating, mWifiManager, config, false);   //WI-FI 연결
            final Dialog dialog = new Dialog(v.getContext());
            dialog.setCanceledOnTouchOutside(false);

            dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            dialog.getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                            | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
            dialog.setContentView(R.layout.dialog);
            dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);


            ConnectivityManager manager = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);    //NullPointerException this line 
            NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if(wifi.isConnected()){
                dialog.dismiss();
            }else {
                dialog.show();
            }
        }
        if (!connResult) {
            Toast.makeText(mFloating, R.string.toastFailed, Toast.LENGTH_LONG).show();
        }

    }

};
  1. try

Context context; //global variable ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); //Null pointer Exception NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if(wifi.isConnected()){ dialog.dismiss(); }else { dialog.show(); }

please perhaps need information, advice for me thanks .

조현욱
  • 31
  • 8

1 Answers1

0

I suggest you use a BroadcastReceiver to wait for wifi connect callback, see How to detect when WIFI Connection has been established in Android?

Community
  • 1
  • 1
Bits
  • 46
  • 1
  • 9
  • first, I try this. perhaps if I don't understands ask for you . thank :) – 조현욱 Mar 24 '16 at 00:25
  • Also if you mean you get null pointer exception on NetworkInfo wifi = ..., you do you have permission to ACCESS_NETWORK_STATE? see: http://developer.android.com/reference/android/net/ConnectivityManager.html#getNetworkInfo(int) , it may return null – Bits Mar 24 '16 at 00:30
  • my wifi-connecter button click class is onCreate method none. so I create – 조현욱 Mar 24 '16 at 00:43
  • `@Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); registerReceiver(broadcastReceiver,intentFilter); }` – 조현욱 Mar 24 '16 at 00:44
  • ` @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) { if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) { dialog.dismiss(); } else { dialog.show();` – 조현욱 Mar 24 '16 at 00:44
  • but I try isConnectedViaWifi(){ `ConnectivityManager connectivityManager this line NPE ERROR – 조현욱 Mar 24 '16 at 00:46
  • If your NetworkInfo wifi is null pointer error, it mean it is not connected, http://developer.android.com/training/basics/network-ops/managing.html you need to check for null, not only isConnected() – Bits Mar 24 '16 at 00:53
  • huh.. I check for null. `ConnectivityManager cm = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); int m_iNetworkType = (activeNetwork == null) ? -1 : activeNetwork.getType();` first,NPE is disappear but `java.lang.IllegalStateException: System services not available to Activities before onCreate()` exception – 조현욱 Mar 24 '16 at 02:05
  • I weired .. my class not oncreate and extends class is abstract class . – 조현욱 Mar 24 '16 at 02:06