Android - Broadcast Receiver works on Emulator Not on Phone

浪子不回头ぞ 提交于 2019-12-24 09:55:58

问题


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

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