Android Eclipse上下文无法在返回类型中解析

时间:2014-09-22 15:10:46

标签: java android eclipse class

我在理解 Context 时遇到问题我编写了一个程序来控制我的灯光,但我需要检查他是否连接错误的WiFi然后使用错误的IP。我没有一个好的解决方案尝试多种选择。我先前看几个关于上下文白化成功的问题。如果您需要完整的程序,我可以发送给您,但我认为这是足够的代码:)

确切的问题是 getCurrentSsid(context) 错误:无法在返回类型中解析上下文

//CLASS SENDCOMMAND

public class SendCommand extends AsyncTask<String, Integer, JSONArray> {


static boolean WifiSSID;


public SendCommand(boolean wifi) {
    this.wifi = wifi;
}

//FUNCTION TO CHECK THE CURREN SSID
public boolean  getCurrentSsid(Context context) {
     ssid = null;
      ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      if (networkInfo.isConnected()) {
        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
        if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
          ssid = connectionInfo.getSSID();
        }
      }
       if (ssid =="Room#2")
          return true;
      else
          return false; 

    }
//wifiSSID = boolean getCurrentSsid(context);

//send the command
@Override
protected JSONArray doInBackground(String... params) {
    // TODO Auto-generated method stub
    Log.d(tag, "Beginnen do in background");
    Log.d(tag, "Wifi is " + wifi);


    HttpClient client = new DefaultHttpClient();
    HttpGet get;


    if (wifi && **getCurrentSsid(context)**){ //HERE HE GIVES A ERROR getCurrentSsid(context)

1 个答案:

答案 0 :(得分:1)

context范围内没有名称为doInBackground的符号。

Context传递给异步任务的常用方法是将其存储在成员变量中并将其传递给构造函数arg:

private Context mContext;

public SendCommand(Context context) {
    mContext = context;
}

然后使用mContext,其中需要Context