Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-18146] Android: Cannot send Broadcast with data property defined in the intent

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionInvalid
Resolution Date2015-07-08T07:44:42.000+0000
Affected Version/sRelease 3.4.1, Release 4.0.0
Fix Version/sn/a
ComponentsAndroid
Labelsn/a
ReporterBenjamin Hatfield
AssigneeAshraf Abu
Created2014-12-06T00:51:39.000+0000
Updated2017-03-17T18:02:55.000+0000

Description

CAVEAT: Cannot tell if this should work or not. Cannot find any official information in the Android docs. Stackoverflow and other topics say you can only send data through extras, which does work in this example. REPRODUCTION: Run the code below in the Genymotion emulator and click the "Send Broadcast" button. RESULTS: Broadcast is not sent. OTHER INFO: Comment out line #9 (data property) and rerun the test. Intent is sent and received.
var win = Ti.UI.createWindow();
var button = Ti.UI.createButton({
	title : 'Send Broadcast'
});
button.addEventListener('click', function() {

	var intent = Ti.Android.createBroadcastIntent({
		action: 'com.appcelerator.action.ALERT',
		data: 'http://www.appcelerator.com'
	});
	Ti.Android.currentActivity.sendBroadcast(intent);
});
win.add(button);
win.open();

var broadcastReceiver = Ti.Android.createBroadcastReceiver({
	onReceived: function(e) {
		Ti.API.info(JSON.stringify(e.intent));
	}
});
Ti.Android.registerBroadcastReceiver(broadcastReceiver, ['com.appcelerator.action.ALERT']);

Comments

  1. Ashraf Abu 2015-07-03

    The 'data' in this case (most likely) is being used incorrectly. The 'data' of an intent is the data to operate on. Check here for clarification: http://developer.android.com/reference/android/content/Intent.html Related readings just for anyone's reference (mainly for me):- http://docs.appcelerator.com/platform/latest/#!/guide/Android_Intents-section-43287298_AndroidIntents-AddExtraData http://docs.appcelerator.com/platform/latest/#!/guide/Android_Broadcast_Intents_and_Receivers
  2. Ashraf Abu 2015-07-03

    I would assume you want to send some form of information or text over. Here's an example how:
       var win = Ti.UI.createWindow();
       var button = Ti.UI.createButton({
       	title : 'Send Broadcast'
       });
       button.addEventListener('click', function() {
        
       	var intent = Ti.Android.createBroadcastIntent({
       		action: 'com.appcelerator.action.ALERT'
       	});
       	intent.putExtra('site', 'http://www.appcelerator.com');
       	intent.putExtra('timestamp', new Date());
       	intent.putExtra(Ti.Android.EXTRA_TEXT, 'Some text that we want to share');
       	Ti.Android.currentActivity.sendBroadcast(intent);
       });
       win.add(button);
       win.open();
        
       var broadcastReceiver = Ti.Android.createBroadcastReceiver({
       	onReceived: function(e) {
       		Ti.API.info(JSON.stringify(e.intent)+" - "+e.intent.getStringExtra('timestamp') +" - "+e.intent.getStringExtra('site')
       	    +" - "+e.intent.getStringExtra(Ti.Android.EXTRA_TEXT));
       		var toast = Ti.UI.createNotification({
       	    message:JSON.stringify(e.intent)+" - "+e.intent.getStringExtra('timestamp') +" - "+e.intent.getStringExtra('site')
       	    +" - "+e.intent.getStringExtra(Ti.Android.EXTRA_TEXT),
       	    duration: Ti.UI.NOTIFICATION_DURATION_LONG
       		});
       		toast.show();
       	}
       });
       Ti.Android.registerBroadcastReceiver(broadcastReceiver, ['com.appcelerator.action.ALERT']);
       
  3. Ashraf Abu 2015-07-03

    [~bhatfield] If the answer is acceptable, can this ticket be closed?
  4. Lee Morris 2017-03-17

    Closing ticket as invalid.

JSON Source