您的位置:首页 > 其它

创建一个WiFi热点

2016-12-28 15:37 204 查看
之前使用perfence臃仲还修改布局麻烦只是简单几个item 后面索性自己重新做一个目前只是个简单实现不能再重新配置后进行重启有好的建议前辈还望指点谢谢!布局简单主要还是反射

public class WifiHot extends Dialog{

private final Context mContext;
private final View mView;
private final SwitchButton mWifi_hot;
private EditText mSsid, mPassWord;
String ssid_Number, password_Number;
public static final String TAG = "WifiHot";
private WifiManager mWifiManager;

public WifiHot(Context context) {
super(context, R.style.FullscreenTheme);
mContext = context;
mView = View.inflate(mContext, R.layout.host_pot, null);
super.setContentView(mView);
mWifi_hot = (SwitchButton) mView.findViewById(R.id.checkbox);
mSsid = (EditText) mView.findViewById(R.id.edi_ssid);
mPassWord = (EditText) mView.findViewById(R.id.edi_PassWord);
mSsid.setText((String) UtilSharedPreferences.get(mContext,"ssid_Number","zma"));
mPassWord.setText((String) UtilSharedPreferences.get(mContext,"password_Number","12345678"));
mWifi_hot.setChecked((Boolean) UtilSharedPreferences.get(mContext,"cheState",true));
mWifi_hot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b == true) {
ssid_Number = mSsid.getText().toString();
password_Number = mPassWord.getText().toString();
Log.i(TAG, "onCheckedChanged: "+ssid_Number+"=="+password_Number);
setWifiApEnable(ssid_Number, password_Number, b);
UtilSharedPreferences.put(mContext,"ssid_Number",ssid_Number);
UtilSharedPreferences.put(mContext,"password_Number",password_Number);
UtilSharedPreferences.put(mContext,"cheState",b);
} else {
UtilWifiHotspot.closeWifiHotPoint(mWifiManager);
UtilSharedPreferences.put(mContext,"cheState",b);
}
}
});
mSsid.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.i(TAG, "beforeTextChanged: ");
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.i(TAG, "onTextChanged: ");
}

@Override
public void afterTextChanged(Editable editable) {
Log.i(TAG, "afterTextChanged: ");
}
});

mSsid.addOnLayoutChangeListener(new View.OnLayoutChangeListener(){
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {

}
});
}

public void setWifiApEnable(String ssid, String password, boolean flag) {
if (ssid_Number == null || mPassWord == null) return;
mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
if ((flag == true) && (mWifiManager.getConnectionInfo() != null)) {
mWifiManager.setWifiEnabled(false);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}

try {
WifiConfiguration config = new WifiConfiguration();
config.SSID = ssid_Number;
config.preSharedKey = password_Number;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);

config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(mWifiManager, config, flag);
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "setWifiApEnable: " + e);
}

}
}

@Override
public void setContentView(int layoutResID) {
}

@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: