无线连接问题

时间:2016-07-21 09:44:10

标签: android android-wifi

关于我在开发应用程序时遇到的wifi问题,我提出了一个问题。更具体地说,我使用一个对话框来选择用户想要连接的网络,但通常在几秒钟(大约1-2)连接之后设备立即断开与我选择的接入点的连接。

这是通过点击工具栏中的compat开关调用的对话框代码:

 public class MyDialog extends DialogFragment {
     Context context;
     WifiManager manager;
     int[] present;
     CharSequence[] chars;
     int c = 0;
     public MyDialog() { }
     public MyDialog( WifiManager manager, Context context, CharSequence[] chars, int[] present) {
          this.context = context;
          this.manager = manager;
          this.present = present;
          this.chars = chars;
      }
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.WifiDialog);
          builder
              .setSingleChoiceItems(chars, 0, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                     c=which;
                }
               })
              .setPositiveButton("Connect", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                   if (present[c] != -1) {
                       manager.disconnect();
                       manager.enableNetwork(present[c], true);
                       manager.reconnect();
                   }
                   Dialog.dismiss();
                   Toast.makeText(context, "Connecting", Toast.LENGTH_SHORT).show();

                   else
                   {
                        context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
                   }
            }})
             .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                        Dialog.dismiss();
                  }})
           .setTitle("Select network");
           .setItems(chars, this)
         return builder.create();
        }
      }

提前谢谢你。 对不起,如果出现问题,我已将代码从c#翻译成java。

0 个答案:

没有答案