Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-10214] TiAPI: Expose broadcast receiver API

GitHub Issuen/a
TypeNew Feature
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2013-02-12T18:51:50.000+0000
Affected Version/sRelease 3.0.0
Fix Version/sRelease 3.1.0, 2013 Sprint 03, 2013 Sprint 04 Core, 2013 Sprint 04
ComponentsTiAPI
Labelscore, module_api, qe-testadded
ReporterRick Blalock
AssigneeAllen Yeung
Created2012-07-27T14:00:40.000+0000
Updated2013-10-23T23:15:37.000+0000

Description

In order to complete this project for the customer we need the receiver API exposed in Titanium - http://developer.android.com/guide/topics/manifest/receiver-element.html Use case: We need to use the broadcast / receiver API to launch a transparent activity that sits on top of the phone app when a phone call is received and during the call. Josh has some more details and a sample native app.

Comments

  1. Neeraj Gupta 2012-08-12

    This feature request has been put on a low priority list following the discussion with services team.
  2. Brian Knorr 2012-08-13

    Why?
  3. Karl Rowley 2012-11-05

    See my example native code in TIMOB-10066.
  4. Karl Rowley 2013-01-03

    The goal stated here is to be able to have JS code that is executed by a broadcast receiver. The broadcast receiver is registered in AndroidManifest.xml for the application. For example: ... That declaration causes the onReceive() method in the TiBroadcastReceiver class to be invoked each time phone state changes. The idea is to have that Java method bring up Kroll and then run some JS code, just as an app starts in Titanium today. So a broadcast receiver looks like this: package com.titanium.broadcastreciever; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; class TiBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Take some action -- start a service or app, or // send a message to a service or app. } } The usage pattern for Broadcast Receivers is generally that the method invoked does something fairly short and then exits. This action could be one of the following things: (1) Start an Android service. (2) Send a message to an Android app or service. (3) Start an Android app. Note that this could lead to unexpected behavior for the user. Right now in Titanium, there isn't any way for a Broadcast Receiver to directly do (1) for a Titanium service. Titanium services can only be started in the context of a Titanium app that is already running.
  5. Allen Yeung 2013-01-31

    Test case: 1. Run a titanium app with the following code snippet app.js
       var win = Ti.UI.createWindow({
       	fullscreen: false,
       	backgroundColor: 'blue'
       })
       
       var bc = Ti.Android.createBroadcastReceiver({
       	url:'mybroadcast.js'
       });
       
       var register = Ti.UI.createButton({
       	top:10,
       	title: 'register broadcast receiver'
       });
       
       var unregister = Ti.UI.createButton({
       	title: 'unregister broadcast receiver'
       });
       
       register.addEventListener('click', function(){
       	Ti.Android.registerBroadcastReceiver(bc, [Ti.Android.ACTION_AIRPLANE_MODE_CHANGED]);
       	Ti.API.info('-------registered broadcast receiver');
       });
       
       unregister.addEventListener('click', function(){
       	Ti.Android.unregisterBroadcastReceiver(bc);
       	Ti.API.info('-------unregistered broadcast receiver');
       });
       
       win.add(register);
       win.add(unregister);
       
       win.open();
       
    mybroadcast.js
       Titanium.API.info("---------------- Hello World!  I am a broadcast ");
       
    2. Click on 'register broadcast receiver' 3. Toggle airplane mode and you should see the hello world message print out. _______________________________________________________________________________ 4. click on 'unregister broadcast receiver' 5. Toggle airplane mode, and you should NOT see the hello world message. ______________________________________________________________________________ 6. Click on 'register broadcast receiver' 7. Back out of the app 8. Toggle airplane mode again, and you should see the hello world message _______________________________________________________________________________ 9. Change the create broadcast call to use the onReceive property and run through steps 1-8 again.
       var bc = Ti.Android.createBroadcastReceiver({
       	onReceived: function() {
       		Titanium.API.info("---------------- Hello World!  I am a broadcast ");
       	}
       });
       
  6. Allen Yeung 2013-02-01

    PR: https://github.com/appcelerator/titanium_mobile/pull/3837
  7. DJ 2013-02-12

    Hi, Trying to use this to receive the android.intent.action.BOOT_COMPLETED into a Titanium App (To then start a small service). Is this possible... struggling to get it working. Thanks!
  8. Anshu Mittal 2013-03-21

    Tested with: SDK:3.1.0.v20130319222852 Studio: 3.1.0.201303091040 Device: Samsung galaxy note(v 2.3.6) Works as expected
  9. DJ 2013-03-26

    Hi - did you get this to work with the BOOT_COMPLETED event? If so, are you able to share any example code? Thanks!
  10. Allen Yeung 2013-03-26

    BOOT_COMPLETED probably won't work with this API since you need the app to be launched first to register the broadcast event. You may have to write a module to hook up the BOOT_COMPLETED broadcast intent.
  11. DJ 2013-03-27

    Thanks - we will be having the App launch first before we need it to accept the BOOT_COMPLETED event. So we don't want that event if the user has never opened the App. Does that make sense? Thanks!
  12. Ben Bahrenburg 2013-04-01

    You might want to see if this module can assist you with your requirement. If not you can always fork and make adjustments as needed. https://github.com/benbahrenburg/benCoding.Android.Tools/blob/master/documentation/bootreceiver.md
  13. DJ 2013-04-02

    Thanks Ben - that looks just right!

JSON Source