Reputation: 1483
I am developing an application which has a countdown timer. I want to pause that timer only if there is an incoming call on the phone. Is there any way to fire an event whenever we receive a call?
Upvotes: 2
Views: 7486
Reputation: 2457
In your Broadcastreceiver onReceive()
write following code
Don't forget to give appropriate permission
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
MyCallStateListener customPhoneListener = new MyCallStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
if (!intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
return;
public class MyCallStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
}
}
}
Upvotes: 0
Reputation: 21
When a Phone-Call is received OS fires a Message, technically called Broadcast.
Any Application can view/Respond to this message via Registering for PhoneIntentReceiver, if more than one Application installed has registered for this, then all of them are given a chance to view this message based on Priority.
You can register for PhoneIntentReceiver via Manifest or Programatically. In both case you specify a Class that extends broadcast receiver in your project, that will receive a callback on detecting an Incoming call.
Then in this class the control is passed on to onReceive method. Its here that you can Stop your Timmer.
This is the story behind it.Happy Coding.
Upvotes: 2
Reputation: 2889
I would say the best implementation would be to utilize time stamps, timers (java.util.Timer or android.app.AlarmManager), and then listen for phone events using a broadcast receiver.
Basically every time you need to start an alarm for a certain period of time store a timestamp for the start of that alarm(probably easiest in an sql db) and then start the timer/alarm. When an alarm goes off make sure to clean up your stored timestamps.
Make sure to listen to phone state changes and on a phone call answered change clear all your alarms/timers and record the stop dates along with the previous timestamps and then when the phone call ends (from your reciever event) restart the timers/alarms for the remaining time.
Upvotes: 0
Reputation: 67
you have to use broadcast receiver for that........
First of all register your receiver in manifest.xml
<receiver android:name="com.cygnet.phonefinder.receiver.PhoneIntentReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
then you have to handle that reciever in
public class PhoneIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) { } }
Upvotes: 0
Reputation: 2534
I think you should extends PhoneStateListener
class.In that class you handle the phone state.For that use the permission in the manifest file for handling phone state (i.e. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
).
And use TelephonyManager
to get the status of phone.
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
manager.listen(this, LISTEN_CALL_STATE); // Registers a listener object to receive notification of changes in specified telephony states.
And override this method.
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:
case TelephonyManager.CALL_STATE_RINGING:
// Here you can perform your task while phone is ringing.
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
Upvotes: 5
Reputation: 4008
You have to write the Broadcast receiver which listens for incoming call
see this link for more info...
Upvotes: 0
Reputation: 1
public class OutgoingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "My Toast", Toast.LENGTH_LONG).show();
}
}
Try This receiver to fire an event
Upvotes: -5