如何编程android来寻找特定的网络? (问题简单的教程) [英] How do I program android to look for a particular network? (Issues Following Simple Tutorial)
问题描述
我试图使用以下示例:
但是由于以下问题,我无法执行:
But I'm unable to execute it due to issues stemming from the line:
WifiManager wifiManager =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
之后,我收到了其他几个错误 - 但是我认为我正好遵循了这个教程/例子。
After that I'm getting several other errors - but [I think] I've followed the tutorial/example precisely.
import java.util.List;
import android.app.Activity;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.content.Context;
public class Connect extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connect);
String networkSSID = "ANDRE-PC_NETWORK";
String networkPass = "superman";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; //ssid must be in quotes
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
conf.preSharedKey = "\""+ networkPass +"\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
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();
break;
}
}
}}
IN ECLIPSE问题日志:
ISSUES IN ECLIPSE PROBLEM LOG:
Description Resource Path Location Type
Cannot make a static reference to the non-static method reconnect() from the type WifiManager Connect.java line 41 Java Problem
Cannot make a static reference to the non-static method disconnect() from the type WifiManager Connect.java line 39 Java Problem
Cannot make a static reference to the non-static method enableNetwork(int, boolean) from the type WifiManager Connect.java line 40 Java Problem
推荐答案
你似乎混淆了一些变量名:
You seem to confuse some variable names:
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.add(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wm.disconnect();
wm.enableNetwork(i.networkId, true);
wm.reconnect();
break;
}
}
wifiManager
应该是 wm
,反之亦然。另外上下文
应该是这个
或者只是作为 getSystemService()
是你的课程的一种方法。
wifiManager
should be wm
or vice versa. Also context
should be this
or just left out as getSystemService()
is a method of your class.
我不确定 add()
的问题。
修改:
嗯 add() 不存在
WifiManager
但 addNetwork()
,所以我想你需要方法而不是 add()
Well add()
does not exist for WifiManager
but addNetwork()
does, so I guess you need that method instead of add()
Edit2 :
你不知道你在做什么,对吧?您甚至不了解类名和变量名之间的区别!
You have no idea what you are doing, right? You don't even understand the difference between a class name and a variable name!
WifiManager wifiManager
< - 首先是类型,也称为类。第二个是变量名。在 for
循环中,您使用类型/类而不是变量,因此只需写入 wifiManager
而不是 WifiManager
。
WifiManager wifiManager
<- First is the Type, also known as class. The second one is the variable name. In your for
loop you use the type/class instead of the variable, so just write wifiManager
instead of WifiManager
.
您应该从理解OOP和Java的基础知识开始。认真地...在尝试写它之前学习语言...
And you should definitively start by understanding the very basics of programming, OOP and Java. Seriously... Learn the language before you try to write it...
这篇关于如何编程android来寻找特定的网络? (问题简单的教程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!