有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

确切的问题在于getCurrentSsid(context)错误:在返回类型中无法解决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) 个答案

  1. # 1 楼答案

    doInBackground的作用域中没有名为context的符号

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

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

    然后在需要Context的地方使用mContext