How do I detect a phone call and execute javascript?

4,155 views
Skip to first unread message

Jac Stofberg

unread,
Jul 22, 2011, 12:59:19 PM7/22/11
to phonegap
Hi there,

I have one really pressing question that needs an answer before I
release my app.

I have written a phonegap based Android application that plays back
audio. I have been trying to detect incoming calls in order to pause
audio when the user receives a call. I'm sure it can't be that
difficult, and yet I have tried and tried to no avail. I've played
around with extending BroadcastReceiver, tried TelephonyManager and
some other solutions that are discussed everywhere. Thing is that I
do not really understand the Broadcast receiver extension or telephony
manager concepts. I've got no Android coding experience (hence I
chose phonegap so that I could code in javascript:)).

How do I create a listener class and implement it in my Droidgap
activity to detect calls and run some javascript when an incoming call
is detected.

PLEASE HELP.

Kind regards,
Jac

Simon MacDonald

unread,
Jul 22, 2011, 1:05:42 PM7/22/11
to phon...@googlegroups.com
I have not tried this but my initial thought is that you can listen
for the PhoneGap "pause" event and in the handler code you could pause
your audio while the user is on a call.

Simon Mac Donald
http://hi.im/simonmacdonald

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com
>

Jac Stofberg

unread,
Jul 22, 2011, 1:16:28 PM7/22/11
to phonegap
Hi Simon,

Thing is that the pause event in phonegap also fires when the screen
locks/phone sleeps. The audio then pauses every 2 minutes:(

Cheers,
Jac

On Jul 22, 10:05 am, Simon MacDonald <simon.macdon...@gmail.com>
wrote:
> I have not tried this but my initial thought is that you can listen
> for the PhoneGap "pause" event and in the handler code you could pause
> your audio while the user is on a call.
>
> Simon Mac Donaldhttp://hi.im/simonmacdonald

Jac Stofberg

unread,
Jul 22, 2011, 1:54:30 PM7/22/11
to phonegap
Simon,

I managed to add a phonelistener to detect calls in my activity and I
can log messages in the console. My only question is, how do I call
my javascript project code from the Android code? Thus, how do I
notify my javascript project inside phonegap of the event when I get
it?

Cheers,
Jac

Devgeeks

unread,
Aug 9, 2011, 3:26:43 AM8/9/11
to phon...@googlegroups.com
Also interested in this...

Would this be basically a phone listener plugin?

Pablo Marti

unread,
Aug 9, 2011, 4:46:30 AM8/9/11
to phon...@googlegroups.com
On Fri, Jul 22, 2011 at 6:54 PM, Jac Stofberg <jacst...@gmail.com> wrote:
> Simon,
>
> I managed to add a phonelistener to detect calls in my activity and I
> can log messages in the console.  My only question is, how do I call
> my javascript project code from the Android code?  Thus, how do I
> notify my javascript project inside phonegap of the event when I get
> it?

You will want to use "sendJavascript". So for example in your JS you
can have onCallReceived method, and in there you will handle the pause
of your audio:

var PhoneListenerPlugin = {
onCallReceived: function() {
// pause media and stuff
}
};

Then in your Java plugin:

this.sendJavascript("navigator.plugins.phoneListener.onCallReceived()");

--
Pablo Martí
http://www.linkedin.com/in/pmarti
python -c "print '706d617267616d40676d61696c2e636f6d'.decode('hex')"

Pablo Marti

unread,
Aug 9, 2011, 4:47:52 AM8/9/11
to phon...@googlegroups.com
On Tue, Aug 9, 2011 at 9:46 AM, Pablo Marti <pma...@gmail.com> wrote:
> On Fri, Jul 22, 2011 at 6:54 PM, Jac Stofberg <jacst...@gmail.com> wrote:
>> Simon,
>>
>> I managed to add a phonelistener to detect calls in my activity and I
>> can log messages in the console.  My only question is, how do I call
>> my javascript project code from the Android code?  Thus, how do I
>> notify my javascript project inside phonegap of the event when I get
>> it?
>
> You will want to use "sendJavascript". So for example in your JS you
> can have onCallReceived method, and in there you will handle the pause
> of your audio:
>
> var PhoneListenerPlugin = {
>    onCallReceived: function() {
>          // pause media and stuff
>    }
> };
>
> Then in your Java plugin:
>
> this.sendJavascript("navigator.plugins.phoneListener.onCallReceived()");

Oops, this should be
this.sendJavascript("window.plugins.phoneListener.onCallReceived()");

Devgeeks

unread,
Aug 10, 2011, 11:02:37 AM8/10/11
to phon...@googlegroups.com
Wow. After a BUNCH of frustration and hacking and deleting and failing and starting over and all of that fun stuff... I have this working as a plugin.

I am no Android dev though, so I am going to spend some more time debugging it and making sure it works, but I have it pausing my radio stream (using PhoneGap Media api) upon incoming or outgoing calls and un-pausing it again when the phone state returns to idle.

Like you I had found enough code on the net to detect the state changes and log them from Android, but had no idea how to get that information to a plugin that could then callback to javascript.


Finally I found the NetworkManager core plugin in the PhoneGap Android source... and between those telephony examples, the ONLY Android PhoneGap plugin that uses a BroadcastReceiver (NetworkManager) and the PhoneGap wiki instructions for writing an Android PhoneGap plugin... EUREKA!

:)

I'll post again in this thread and whack it up on GitHub once I think it's game for others to test, etc.

This has been a blast of a journey so far. Porting one iOS app from Objective-C to PhoneGap and then trying to get that same app working in PhoneGap on Android... whew! 

Simon MacDonald

unread,
Aug 10, 2011, 11:11:44 AM8/10/11
to phon...@googlegroups.com
Lemme know if you want to do a code review. I did the NetworkManager plugin using BroadcastReceiver and I'm working on a Battery class using BroadcastReceiver as well.

--

Devgeeks

unread,
Aug 10, 2011, 11:24:21 AM8/10/11
to phon...@googlegroups.com
Simon,

I think I'd like that very much. 

Let me play with it a bit (so far tested only a few scenarios, it's after 1am after all, heh) then I think I'd be up for it. I am at best a hobbyist iOS dev. The last week or two has been my very first exposure to Android, heh. I trust my iOS PhoneGap plugins more or less (VolumeSlider for example), but this? heh. Not so much.

Simon MacDonald

unread,
Aug 10, 2011, 11:26:14 AM8/10/11
to phon...@googlegroups.com
No worries, when you feel comfortable push it up to a github repo and send me the link and I'll take a look for you. Also, go to bed!

--

Devgeeks

unread,
Aug 14, 2011, 7:12:30 AM8/14/11
to phon...@googlegroups.com
Sent. Thanks again, Simon.

Devgeeks

unread,
Oct 22, 2012, 1:25:41 AM10/22/12
to phon...@googlegroups.com
Audio should stop playing when the phone rings now... as of PhoneGap 1.3 my plugin became pointless...


Are you saying it's started happening again?


Or are you just looking for a way to detect incoming calls?



My plugin was last updated for PhoneGap 1.6, so it might need some tweaking :/



On Sunday, October 21, 2012 4:45:57 AM UTC+11, Felix Maina wrote:
Hi there thank you for posting this question. I would really like the solution you found for this problem i have been working on it for the last week with no success. Could you please post or send me the code on how you succeeded.

Felix Maina

unread,
Oct 22, 2012, 9:04:10 AM10/22/12
to phon...@googlegroups.com

Ok audio still plays when a phine call comes in  but what worked for me is adding the pause and resume events listeners.

--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com
 
To compile in the cloud, check out build.phonegap.com
 
 

Alessandro Nunes

unread,
Apr 23, 2013, 8:35:15 AM4/23/13
to phon...@googlegroups.com
Hello,

I`m using the build.phonegap.com, so I cannot use other plugins.
There is any native solution for Phonegap 2.3 / 2.5?

Thanks!




Alessandro Nunes

unread,
Apr 23, 2013, 8:42:10 AM4/23/13
to phon...@googlegroups.com
Hello,

I'm developing a radio App, and it should continue playing when the device pauses, but not on a phone call.

Anybody have a solution for build.phonegap.com using phonegap 2.3 or 2.5?

Thanks! 

Андрей Ясинский

unread,
Jan 11, 2014, 4:48:13 PM1/11/14
to phon...@googlegroups.com
Plugin for Andriod works fine. Is there the same for iPhone? Does anybody know?
Reply all
Reply to author
Forward
0 new messages