问题
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.
回答1:
You have had not added the parameters for the intent filter, the final manifest should look like this
<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>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
</intent-filter>
</receiver>
</application>
Hope this helps.
回答2:
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>
回答3:
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
.
回答4:
Add this permission also in order to check the network state of your device.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
回答5:
a mix-and-match of various previous answers, these snippets worked for me to check IF I AM CONNECTED TO A WIFI NETWORK (this does not tell me if i am connected to internet):
create a WifiMonitor.java class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;
public class WifiMonitor extends BroadcastReceiver {
private String TAG = "TGtracker";
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG,"entered wifimonitor");
// Process the Intent here
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()==true) {
Log.d(TAG, "You are connected to WIFI "+wifi.getConnectionInfo());
} else {
Log.e(TAG,"You are NOT connected to WIFI");
}
}
}
now go to your Manifest file and include
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
and finally, place the receiver right before the < / application > tag (still in Manifest file)
<application...
<receiver android:name=".WifiMonitor" android:enabled="true">
<intent-filter>
<action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
</intent-filter>
</receiver>
</application>
when applying your code, DO NOT COPY < application... or < / application > tags in the code above (i just put them there so that you will know where to place the receiver).
NOTE: please fix your MainActivity to extend Activity and not BroadcastReceiver.
回答6:
For me the changes in the AndroidManifiest.xml did not work. I have the same problem, the application stopped suddenly.
I used the registerReceiver in the main activity and it works:) For me the WifiStateTester is the class that extends from BroadcastReceiver.
WifiStateTester wfs= new WifiStateTester();
IntentFilter iFilter= new IntentFilter();
iFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
iFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
this.registerReceiver(wfs, iFilter);
Other different thing I did, is that in the onReceiver method I directly checked the wifi status.
来源:https://stackoverflow.com/questions/7329682/unable-to-listen-to-android-wi-fi-managers-state