2013-11-15 3 views
0

내 앱 사용자 중 일부는 배터리가 빨리 소모되었다고 말합니다. 나는 그들이 GPS와 와이파이가 켜져있을 때 그것을 결정했다.휴대 전화가 스캐닝 또는 와이파이 연결 여부를 감지하는 방법

다음 코드는 Wi-Fi의 사용 여부를 결정한다고 생각합니다. 항상 리턴은 연결되지 않습니다. 웬일인지 나의 전화는 사무실 wifi에 연결할 수 없다. 그래서 그 숫자.

내가 원하는 것은 휴대 전화 설정에서 무선 랜이 켜져 있는지 감지 할 수있는 것입니다.

내가 맞는지 확실하지 않지만 Wi-Fi가 켜져 있지만 연결되어 있지 않으면 배터리가 여전히 소모됩니까?

와이파이가 켜지거나 꺼져 있는지 알려주는 코드가 있습니까?

+0

@OlafDietsche 죄송합니다. – turtleboy

답변

1

이 코드는 Android 기기에서 Wi-Fi가 사용 설정되어 있는지 확인하는 데 도움이됩니다.

WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
if (wifi.isWifiEnabled()){ 
//wifi is enabled 
} 

wifi.getWifiState()을 사용하여 Wi-Fi의 현재 상태를 확인할 수도 있습니다.

+0

완벽! 고마워. – turtleboy

0

기기가 작동중인 네트워크에 연결되어 있는지 확인하는 데이 클래스를 사용합니다. 이 클래스는 가능한 모든 인터넷 제공 업체에서 작동합니다.

public class ConnectionDetector { 

private Context _context; 

public ConnectionDetector(Context context){ 
    this._context = context; 
} 

/** 
* Checking for all possible internet providers 
* **/ 
public boolean isConnectingToInternet(){ 
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (connectivity != null) 
     { 
      NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
      if (info != null) 
       for (int i = 0; i < info.length; i++) 
        if (info[i].getState() == NetworkInfo.State.CONNECTED) 
        { 
         return true; 
        } 

     } 
     return false; 
} 

}

// 연결 검출기

ConnectionDetector의 CD = 새로운 ConnectionDetector (getApplicationContext());

그리고 isConnectingToInternet() 메소드를 인터넷 연결을 확인할 ConnectionDetector 클래스의 객체로 호출하십시오. 인터넷이 작동 중이면 false를 반환하고 그렇지 않으면 false를 반환합니다.

// Check if Internet present 
    if (!cd.isConnectingToInternet()) { 
     // Internet Connection is not present 
     alert.showAlertDialog(
       MainActivity.this, 
       "Alert!!", 
       "Internet Connection is not on.Please check your network.", 
       false); 
     // stop executing code by return 
     return; 
    } 
관련 문제