Android Lollipop发送多个BroadcastReceivers用于电话状态更改

时间:2015-02-02 13:17:58

标签: android android-broadcast

Android kitkat手机状态广播接收器工作正常。在android lolipop手机状态广播接收器发送多个广播。 Android Lolipop是否有任何改变。

public class PhoneStateBroadcastReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    Log.d("PhoneState", state);
  }
 }  
} 

  <receiver android:name="com.phonestate.PhoneStateBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

2 个答案:

答案 0 :(得分:0)

在Lollipop,每个手机状态变化事件似乎都会出现两次。本文介绍了该问题,并为其提供了一个简单的解决方法: http://mmarvick.github.io/blog/blog/lollipop-multiple-broadcastreceiver-call-state/

答案 1 :(得分:0)

我会推荐这个解决方案:

public void onReceive(Context context, Intent intent) {
  long subId = intent.getLongExtra("subscription", Long.MIN_VALUE);
  if(subId < Integer.MAX_VALUE) {
    // hurray, this is called only once on all operating system versions!
  }
}

它适用于4.x和5.x,并且应该是向前兼容的。有关详细信息,请参阅我的博客:

http://www.skoumal.net/en/android-duplicated-phone-state-broadcast-on-lollipop/