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

Kam*_*han 6 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>
Run Code Online (Sandbox Code Playgroud)

gin*_*ngo 1

我会推荐这个解决方案:

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!
  }
}
Run Code Online (Sandbox Code Playgroud)

它适用于 4.xa 5.x 并且应该向前兼容。更多详情请参考我的博客:

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