Reputation: 738
I am working on a app that runs in the background and listens to incoming calls.
For this I have created a service which calls TelephonyManager.listen in the onHandleIntent method.
Unfortunately although the constructor of the phonestatelistener is invoked, its onPhoneStateChanged method is not getting invoked.
Making the same call from a activity works fine. I am confused what the issue might be. I have searched many similar posts but none of them have answered my question satisfactorily. Hence I am posting this question.
Following is my service and phonelistener implementation:
public class PhoneListenersService extends IntentService{
TelephonyManager tm;
CallStateListener callstatelistener;
public PhoneListenersService() {
super("PhoneListenersService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
int count=0;
do
{
TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
TelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
Log.d("Count", ""+count++);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while(count<100);
}
class TeleListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// CALL_STATE_IDLE;
Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK;
Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
// CALL_STATE_RINGING
Toast.makeText(getApplicationContext(), incomingNumber,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
}
Please help me.
Thanks!
Upvotes: 0
Views: 1070
Reputation: 738
Left with no option, I have changed my approach. I use Broadcast Receiver with Intent Service to listen for incoming calls. Following is the code if it helps someone:-
public class PhoneListenersService extends IntentService{
TelephonyManager tm;
BroadcastReceiver receiver;
IntentFilter filter;
public PhoneListenersService() {
super("PhoneListenersService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
//Intent
Filter filter=new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
//Create instance of BroadcastReceiver
receiver=new BroadcastReceiver()
{
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
if (bundle == null)
return;
// Incoming call
String state =
bundle.getString(TelephonyManager.EXTRA_STATE);
if ((state != null) &&
(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)))
{
phoneNumber =
bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
//if phoneNumber is not blank get location details
if(!phoneNumber.isEmpty())
{
//Do work here
}
}
}
};
registerReceiver(receiver, filter);
} }
Regards.
Upvotes: 1