Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 242
Kamalanathan Avatar asked Feb 02 '15 13:02

Kamalanathan


1 Answers

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/

like image 160
gingo Avatar answered Sep 27 '22 18:09

gingo