android.net.WifiManager cannot be resolved to a variable

折月煮酒 提交于 2019-12-13 09:04:46

问题


I have this class in my MainActivity to detect Internet Connection and wifi scanning result. I am getting at the Moment this error "android.net.wifi.SCAN_RESULTS cannot be resolved to a type" at this

inner MainActivity:

class Receiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(
					android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {

			} else if(intent.getAction().equals(android.net.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { //here is the error
                 }
	    }
	}

        <receiver android:name=".ConnectionBroadcast" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.net.wifi.SCAN_RESULTS" />
            </intent-filter>
        </receiver>

回答1:


Just try this The best that worked for me:

AndroidManifest

<receiver android:name="com.AEDesign.communication.WifiReceiver" >
   <intent-filter android:priority="100">
      <action android:name="android.net.wifi.STATE_CHANGE" />
   </intent-filter>
</receiver>

BroadcastReceiver class

public class WifiReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {

      NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
      if(info != null) {
         if(info.isConnected()) {
            // Do your work. 

            // e.g. To check the Network Name or other info:
            WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            String ssid = wifiInfo.getSSID();
         }
      }
   }
}

Permissions

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


来源:https://stackoverflow.com/questions/29487366/android-net-wifimanager-cannot-be-resolved-to-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!