如何编程android来寻找特定的网络? (简单教程后面的问题)

时间:2013-04-03 09:00:46

标签: java android eclipse wifimanager

我正在尝试使用以下示例:

How do I program android to look for a particular network?

但是由于来自该行的问题,我无法执行它:

WifiManager wifiManager =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);

之后我得到了其他几个错误 - 但是[我认为]我已经完全按照教程/示例进行了操作。

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;
            }           
         }

    }}

ECLIPSE问题日志中的问题:

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

1 个答案:

答案 0 :(得分:0)

您似乎混淆了一些变量名称:

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,反之亦然。另外,context应为this,或者只是遗漏,因为getSystemService()是您班级的一种方法。

我不确定add() ...

的问题

修改

add()并不存在WifiManageraddNetwork()不存在,所以我猜你需要这种方法而不是add()

<强> EDIT2

你不知道自己在做什么,对吗?你甚至不理解类名和变量名之间的区别!

WifiManager wifiManager&lt; - 首先是Type,也称为class。第二个是变量名。在for循环中,您使用的是类型/类而不是变量,因此只需编写wifiManager而不是WifiManager

你应该明确地从了解编程,OOP和Java的基础知识开始。说真的......在你尝试编写语言之前先学习一下语言......