2014-09-22 26 views
0

我有問題了解上下文我編寫了一個程序來控制我的指示燈,但我需要檢查他是否沒有連接錯誤的WiFi,然後使用錯誤的IP。我嘗試多種選擇沒有一個好的解決方案。我期待了一些關於背景枯竭成功的問題。如果您需要完整的程序,我可以將它發送給你,但我認爲這是足夠的代碼:)Android Eclipse上下文無法解析返回類型

確切的問題是getCurrentSsid(上下文)錯誤:上下文無法在返回類型來解決

//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) 
+2

什麼問題? – 2014-09-22 15:16:16

+0

對不起,我編輯了它。 – 2014-09-22 15:34:18

+0

上下文變量設置在哪裏? O_o – 2014-09-22 15:37:52

回答

1

在您的doInBackground範圍內沒有符號名稱context

Context傳遞到一個異步任務的常見方式是將其存儲在一個成員變量和在構造ARG傳遞:

private Context mContext; 

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

,然後使用mContext其中需要Context

+0

我以前試過! Eclipse不會給出錯誤,但我的應用程序不斷停止,當我按下一個按鈕打開燈... – 2014-09-22 15:52:55

+0

09-22 17:51:45.628:W/dalvikvm(15218):threadid = 11:線程退出與未捕獲的異常(group = 0x40ea76d8) – 2014-09-22 15:53:48

+0

是的,代碼中還有其他問題... – laalto 2014-09-22 16:18:11

相關問題