Unable to listen to android wi-fi manager's state?

17,823

Solution 1

If you did not add the following permissions then try again after adding those.

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

Solution 2

First, you should make sure you request the permission to get access to view WIFI state. Just do as Anup Rojekar said.

Second, I believe you make mistake in using a BroadcastReceiver. In your manifest.xml, you declare the MainActivity.

<activity 
        android:name=".MainActivity"
            android:label="@string/app_name">
    <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

so the MainActivy is an Activity. Also, you declare the MainActivity as a broadcastReceiver. This is illegal. You should use a standalone broadcast to do so. Like this:

           <receiver android:name=".WifiBroadcastReceiver">
<intent-filter> 
     <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
</intent-filter>
       </receiver>  

Please notice that I changed the name for the broadcastReceiver and inside the node, you should add a <action> node like above.

Third, create a WifiBroadcastReceiver.java and copy the codes from your original MainActivity.java to it. Remember to change the class declaration to public class WifiBroadcastReceiver extends BroadcastReceiver

Hope I could help. Please tell me if it works. I don't have a pc on hand; thus I don' t test the codes either.

Also, you need a new MainActivity.java file. It could be generated automatically in Eclipse. This class should be declared like: public class MainActivity extends Activity.

Solution 3

Add this permission also in order to check the network state of your device.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Share:
17,823

Related videos on Youtube

Vinoth
Author by

Vinoth

I love programming and I love designing. Puzzles are like little children waiting to be picked up and getting pampered with love. I am a freelancer and work with a competitive edge.

Updated on June 04, 2022

Comments

  • Vinoth
    Vinoth about 2 years

    I am having some trouble with BroadCast receiver for checking the Wi-fi state. Could you please help?

    This is my manifest file.

    <uses-permission android:name="android.permission.READ_PHONE_STATE"> </uses-permission>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"> </uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"> </uses-permission>
    <uses-permission android:name="android.permission.INTERNET"> </uses-permission>
    <application 
            android:icon="@drawable/icon" 
            android:label="@string/app_name">
        <activity 
            android:name=".MainActivity"
                android:label="@string/app_name">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
               <receiver android:name=".MainActivity">
    <intent-filter android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
           </receiver>  
       </application>
    </manifest>
    

    And this is the MainActivity.java

    public class MainActivity extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1);
            String msg = null;
            switch (state) {
            case WifiManager.WIFI_STATE_DISABLED:
                msg = "it is disabled";
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                msg = "it is enabled";
                break;
            case WifiManager.WIFI_STATE_DISABLING:
                msg = "it is switching off";
                break;
            case WifiManager.WIFI_STATE_ENABLING:
                msg = "wifi is getting enabled";
                break;
            default:
                msg = "not working properly";
                break;
            }
            if (msg != null) {
                Log.d("************%%%%%%%%wifi state ", "WIFI" + msg);
                Toast.makeText(context, "Wifi state is" + msg, Toast.LENGTH_LONG)
                        .show();
            }
        }
    }
    

    I am unsure where am I making my mistake. Any input would be appreciated.
    I am not getting any errors just that the log file doesn't show the required message.

  • Vinoth
    Vinoth almost 13 years
    Hi Anup I've added the permission to the manifest. And now the app shuts down unexpectedly. While referring to the log it said "Uncaught handler, thread main exit due to uncaught exception" "java.lang.RuntimeException. Unable to instantiate activity". Could you please let me know where could the problem be arising. Thanks
  • Vinoth
    Vinoth almost 13 years
    I've just done as you said and my application forces shutdown. I've explained it in the previous comment please take a look at it and let me know where am I causing an error.
  • Vinoth
    Vinoth almost 13 years
    Your explanation cleared my doubts on how to edit the manifest file. Thanks Huang. I am testing it now, will let you know how it works