Kamalanathan
Kamalanathan

Reputation: 1747

Android Lollipop Sends Multiple BroadcastReceivers for Telephone State Changes

Up to android kitkat phone state broadcast receiver works fine. In android lolipop phone state broadcast receiver sending multiple broadcast. Is there any thing changed in 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>

Upvotes: 6

Views: 727

Answers (1)

gingo
gingo

Reputation: 3169

I would recommend this solution:

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!
  }
}

it works on both 4.x a 5.x and should be forward-compatible. For more details please refer to my blog:

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

Upvotes: 1

Related Questions