This question already has answers here:
How to start new activity on button click
(22个答案)
2年前关闭。
我不知道为什么订购下一个活动时我的应用程序崩溃,尽管它在其他应用程序中也能正常工作。
基本上,我希望用户连接到特定的wifi(我在
如果已连接,则他应进入主要活动,否则他将无法访问其他活动。有什么问题,如果用户连接到wifi,还有什么更好的身份验证方法?
确保像这样注册和注销接收者:
当我们执行此操作时,我们应该使用ConnectivityManager.getActiveNetworkInfo()来获取NetworkInfo,它又具有内部枚举NetworkInfo.State#CONNECTED。
我们可以使用该枚举来检查
(22个答案)
2年前关闭。
我不知道为什么订购下一个活动时我的应用程序崩溃,尽管它在其他应用程序中也能正常工作。
基本上,我希望用户连接到特定的wifi(我在
WIFIConfiguration
中提到过)如果已连接,则他应进入主要活动,否则他将无法访问其他活动。有什么问题,如果用户连接到wifi,还有什么更好的身份验证方法?
wifi.java
类public class WiFiConfiguration extends Activity {
Button btnnext;
public String networkSSID = null;
public String networkPass = null;
public Button ConnectButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_wifi_configuration);
btnnext = (Button) findViewById(R.id.btnnext);
ConnectButton = (Button)findViewById(R.id.connButton);
/*btnnext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(WiFiConfiguration,this secondActivity.class);
startActivity(i);
}
});*/
ConnectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
networkSSID = "myssid";
networkPass = "mypass";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain ssid in quotes
/* for wep*/
//conf.wepKeys[0] = "\"" + networkPass + "\"";
//conf.wepTxKeyIndex = 0;
//conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
/* for wpa*/
conf.preSharedKey = "\""+ networkPass +"\"";
/* for open network*/
//conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
Context context = getApplicationContext();
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list )
if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
while (wifiInfo.getSSID() == null) {
Log.i("WifiStatus", "Here I am");
try {
Thread.sleep(Time.SECOND);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wifiInfo = wifiManager.getConnectionInfo();
}
System.out.println("Connection established");
Toast.makeText(context, "Connection established", 1000).show();
Intent i = new Intent(WiFiConfiguration,this secondActivity.class);
startActivity(i);
break;
}
}
});
}
}
最佳答案
我认为WifiManager#reconnect()
是异步调用。因此,我们应该使用WifiManager#WIFI_STATE_CHANGED_ACTION操作注册广播接收器。请参考以下片段:
private BroadcastReceiver mWifiStateChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
String action = intent.getAction();
if (!TextUtils.isEmpty(action)) {
if (action.equalsIgnoreCase(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
// just to be more sure, you can also check whether the connected APN as same as the required one
// by comparing APN names
if (networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
Intent i = new Intent(context, secondActivity.class);
startActivity(i);
}
}
}
}
}
}
}
};
确保像这样注册和注销接收者:
@Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
this.registerReceiver(mWifiStateChangeReceiver, intentFilter);
}
@Override
protected void onStop() {
super.onStop();
this.unregisterReceiver(mWifiStateChangeReceiver);
}
当我们执行此操作时,我们应该使用ConnectivityManager.getActiveNetworkInfo()来获取NetworkInfo,它又具有内部枚举NetworkInfo.State#CONNECTED。
我们可以使用该枚举来检查
WifiManager#reconnect()
调用是否已完成,并且我们已经连接到APN,并且检查之后,我们可以在此处开始新的活动。10-08 17:45