Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-19328] Android: broadcast receiver always executed twice for one broadcast

GitHub Issuen/a
TypeBug
PriorityMedium
StatusReopened
ResolutionUnresolved
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labelsn/a
ReporterShawn Lan
AssigneeHieu Pham
Created2015-07-31T02:25:14.000+0000
Updated2015-11-13T02:46:09.000+0000

Description

It was working fine. Recently I found this bug, but not sure starting which SDK version the problem existed. Create a default Alloy project with the following: index.xml
<Alloy>
	<Window class="container" onClose="doClose" />
</Alloy>
index.js
function doClose() {
	Ti.Android.unregisterBroadcastReceiver(bc);
}

$.index.open();

var onReceived = function(e) {
	Ti.API.info("received!");
};
var bc = Ti.Android.createBroadcastReceiver({
	onReceived : onReceived
});
Ti.Android.registerBroadcastReceiver(bc, ["android.intent.action.PHONE_STATE"]);
tiapp.xml (use the following for the android section)
   <android xmlns:android="http://schemas.android.com/apk/res/android">
    	<manifest>
            <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    	</manifest>
    </android>
After launching the app, call to the cellphone and see the onReceived function executed twice for one broadcast.

Comments

  1. Hieu Pham 2015-08-25

    This isn't a bug. The phone_state broadcast supposed to fire every time the phone's state changes: http://developer.android.com/reference/android/telephony/TelephonyManager.html#ACTION_PHONE_STATE_CHANGED So if you are receiving a phone call, there are multiple phone states (i.e, CALL_STATE_IDLE, CALL_STATE_OFFHOOK, and CALL_STATE_RINGING). I've also tested with android.intent.action.AIRPLANE_MODE, and it works as expected (only fired once)
  2. Shawn Lan 2015-08-25

    For each phone state the receiver is called twice!! The bug seems to only happen with phone state. Please test using the test case under the specified environment. Please re-open this case or I'll need to create another case.
  3. Chee Kiat Ng 2015-11-12

    Please use the sample code to regenerate the issue. index.xml
       <Alloy>
       
       	<Window class="container" onClose="doClose" />
       
       </Alloy>
       
    index.js
       function doClose() {
       
       	Ti.Android.unregisterBroadcastReceiver(bc);
       
       }
       
        
       
       $.index.open();
       
        
       
       var onReceived = function(e) {
       
       	Ti.API.info("received!");
       
       };
       
       var bc = Ti.Android.createBroadcastReceiver({
       
       	onReceived : onReceived
       
       });
       
       Ti.Android.registerBroadcastReceiver(bc, ["android.intent.action.PHONE_STATE"]);
       
    tiapp.xml (use the following for the android section)
          <android xmlns:android="http://schemas.android.com/apk/res/android">
       
           	<manifest>
       
                   <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
       
           	</manifest>
       
           </android>
       
    Let's have one more look at this.
  4. Chee Kiat Ng 2015-11-12

    Sorry just realised the sample code commented above is exactly the same as ticket description.
  5. Ashraf Abu 2015-11-12

    Tested this with an Emulator with Android 5.1.0 using the above code. Simulated an inbound call by running the following commands based on http://developer.android.com/tools/devices/emulator.html#telephony 1) telnet localhost 5554 2) gsm call 1111 Broadcast receiver executed once when Picking up phone call. Broadcast receiver executed once more when phone call ended and went back to app. Unable to reproduce broadcast receiver executing twice.
  6. Shawn Lan 2015-11-12

    Have you tested on a device? I tested it on my HTC One M7. Call pickup executes twice the receiver. Call end executes twice the receiver as well.
  7. Ashraf Abu 2015-11-13

    [~shawnlan] May I know which Android version HTC One M7 is running?
  8. Shawn Lan 2015-11-13

    5.0.2
  9. Ashraf Abu 2015-11-13

    Thanks. I'll try to test on something similar to that.

JSON Source