问题
I have written a simple app which atm just pops up a toast message when the network state changes. This works perfectly on the emulator but I've tried it on 2 different android phones and it seems like the broadcast reciever is never trigger on these events. This is my broadcast reciever:
public class PhoneStateReceiver extends BroadcastReceiver
{
@Override
public void onReceive( Context context, Intent intent )
{
Toast.makeText( context, "Hi", Toast.LENGTH_SHORT ).show();
}
}
This is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
..
<uses-sdk android:minSdkVersion="8" />
<application
..
<receiver android:name=".PhoneStateReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"></action>
</intent-filter>
</receiver>
..
</application>
<!-- Needed to check when the network connection changes -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
Anyone know why this is happening or what I might have done wrong?
Thanks, Rob
回答1:
I can't find anything obviously incorrect in your code, so here is a best-effort.
In your manifest place the block AFTER the
PVS
来源:https://stackoverflow.com/questions/10154893/android-broadcast-receiver-works-on-emulator-not-on-phone