我怎么做WI-FI连接时,在android上显示进度对话框?

时间:2016-03-24 00:16:35

标签: android

最近我有WI-FI连接器代码。 如果wi-fi连接按钮单击,则显示对话框。 但显示对话仍然存在。 我希望当WI-FI连接显示对话框,并完成wifi连接,关闭对话框。

我试试这个。但我不知道错误logcat。

如何以无线方式连接时以编程方式显示进度对话框?

@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. 试试
  2. 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(); }

    请或许需要我的信息和建议 谢谢 。

1 个答案:

答案 0 :(得分:0)

我建议您使用BroadcastReceiver等待wifi连接回调,请参阅How to detect when WIFI Connection has been established in Android?