I am working on an application where i need to finish the activity from onResume if there is an incoming call. I have created a CallHelper class where i am detecting any incoming call using PhoneStateListener.
public class CallHelper {
/**
* Listener to detect incoming calls.
*/
private static class CallStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
System.out.println("RINGING");
OverlayActivity overlayActivity = OverlayActivity.getInstance();
overlayActivity.finish();
System.out.println("Activity has been closed!!");
break;
}
}
}
/**
* Broadcast receiver to detect the outgoing calls.
*/
public class OutgoingReceiver extends BroadcastReceiver {
public OutgoingReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(ctx,
"Outgoing: "+number,
Toast.LENGTH_LONG).show();
}
}
private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;
private OutgoingReceiver outgoingReceiver;
public CallHelper(Context ctx) {
this.ctx = ctx;
callStateListener = new CallStateListener();
outgoingReceiver = new OutgoingReceiver();
}
/**
* Start calls detection.
*/
public void start() {
tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
ctx.registerReceiver(outgoingReceiver, intentFilter);
}
/**
* Stop calls detection.
*/
public void stop() {
tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
ctx.unregisterReceiver(outgoingReceiver);
}
}
I need to detect inside onResume, if there is any call, i need to finish the activity if not i don't need to do anything.
I haven't found the solution yet, so what i did is, i have added the code for detecting the CallStateChange inside my Activity's onResume and using that code i am executing the task which i want to execute on CallStateChange.
I am not sure if this is a correct solution or not, but would be expecting views on the same from the community.
Here's the code which i am using:
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING){
// Code i wanted to execute
}
}
};
telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
Related
I'm having an issue in trying to run a PhoneStateListener on* callback methods in a backround thread. Here's what I did so far:
First, I wrote my PhoneStateListener implementation. Something on the lines of:
public MyPhoneStateListener extends PhoneStateListener {
private TelephonyManager mTelephonyManager;
public MyPhoneStateListener(Context context) {
mTelephonyManager = (TelephonyManager) context.getSystemServices(Context.TELEPHONY_SERVICE);
}
#Override
public void onCallStateChanged(int state, String incomingNumber) { ... }
public void startListening() {
mTelephonyManager.listen(this, PhoneStateListener.LISTEN_CALL_STATE);
}
}
Then I created a Handler class:
public PhoneStateHandler extends Handler {
private Context mContext;
private MyPhoneStateListener mListener;
public static final int MESSAGE_START = 0;
public PhoneStateHandler(Context context, Looper looper) {
super(looper);
mContext = context;
}
#Override
public void onHandleMessage(Message message) {
if (mListener == null) {
mListener = new MyPhoneStateListener(mContext);
}
switch (message.what) {
case MESSAGE_START:
mListener.startListening();
break;
}
}
}
Finally, from within a Service I start a new HandlerThread and attach my PhoneStateHandler to its' looper:
HandlerThread mThread = new HandlerThread("PhoneStateListenerThread");
mThread.start();
PhoneStateHandler mHandler = new PhoneStateHandler(this, mThread.getLooper());
mHandler.sendMessage(Message.obtain(mHandler, PhoneStateHandler.MESSAGE_START));
What I expect, is that methods such as MyPhoneStateListener.onCallStateChanged() to be executed on the mThread thread instead of the UI thread. I'm exploiting the fact that when a PhoneStateListener is first created, it is bound to the Looper of its current thread. This is undocumented, but that's what actually happens.
What I observed, however, is that despite the PhoneStateHandler.onHandleMessage() method gets called in the HandlerThread's looper (as I expect), the MyPhoneStateListener.onCallStateChanged() still gets called in the UI thread.
Any clue about what's wrong with my code?
Thanks
I want to detect when the outgoing call has started and ended, and want to start an service when call has started and end the service when call has ended.
Here's the code i got from another question, but don't know where to put the start and stop activity to make it work.
public class CallListener {
private CallStateListener callStateListener = null;
private OutgoingReceiver outgoingReceiver = null;
public CallListener(Context context) {
this.context = context;
callStateListener = new CallStateListener();
outgoingReceiver = new OutgoingReceiver();
}
private class CallStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
doSomething1();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
doSomething2();
break;
case TelephonyManager.CALL_STATE_RINGING:
doSomething3();
break;
}
}
}
public class OutgoingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
doSomething4();
}
}
public void start() {
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
context.registerReceiver(outgoingReceiver, intentFilter);
}
public void stop() {
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
context.unregisterReceiver(outgoingReceiver);
}}
And here's to add in the service to make it work:
public class MyService extends Service {
private CallListener call = null;
public MyService() {
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
call = new CallListener(getApplicationContext());
call.start();
return(START_STICKY);
}
#Override
public void onDestroy() {
call.stop();
call.onDestroy();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
Thanks so much for any help, i may know that this is easy but i just don't know.
Thanks and have a really good day..!! :)
I had already searched how to detect call events like ringing / idle / etc... no success :(
Unfortunately, there is no solution in Android at this time...
This is well explained in this previous question/summary post:
Detecting outgoing call answered on Android
I have searched for two days, but hothing have found
Is it possible to detect state of outgoing call programmaticaly (answered, busy or droped)?
Thank you for your answers.
*// Add receiver to manifest file
public class OutgoingCallReceiver extends BroadcastReceiver {
private static long timeStarted = -1L; // IMPORTANT!
private static String number;
private static boolean noCallListenerYet = true;
#Override
public void onReceive(final Context context, Intent intent) {
PhoneCallListener phoneListener = new PhoneCallListener(context);
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
private class PhoneCallListener extends PhoneStateListener {
Context context;
private boolean isPhoneCalling = false;
public PhoneCallListener(Context context2) {
// TODO Auto-generated constructor stub
context=context2;
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
//do something here
}
if (state == TelephonyManager.CALL_STATE_IDLE && timeStarted != -1L) {}
}
}
}
I need a way to get the the status when a outgoing call is answered. However, in the OFFHOOK state I am also using to call for the outgoing call(ACTION_CALL). How can I add the awnsered state without overriding the outgoing call activity?
public class OutgoingBroadcastReceiver extends BroadcastReceiver {
private Intent mIntent;
private String phonenumber = null;
public static boolean wasRinging;
#Override
public void onReceive(Context context, Intent intent) {
mIntent = intent;
MyPhoneStateListener phoneListener = new MyPhoneStateListener(context);
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
public class MyPhoneStateListener extends PhoneStateListener {
private final Context context;
public MyPhoneStateListener(Context context) {
this.context = context;
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.e("%%%%%%%%%%%%%%%%%%%%%%%%%%%t", "OFFHOOK");
if (UIUDialer.isOutgoingCall() == true) {
//Do my work when outgoing call is detected
}
else if (!wasRinging)
{
Log.e("%%%%%%%%%%%%%%%%%%%%%%%%%%%t", "WASRINGING");
//Do my work when outgoing call is awnsered
}
context.sendBroadcast(new Intent("finish_incoming"));
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_RINGING:
wasRinging = true;
break;
}
}
}
}
There is no public API available for this.
Why don't you just use the boolean wasRinging?
(you'll have to make it static and remove the initialization, though)
This is my BroadcastReceiver
public class PlayAudio extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}
}
This is my Custom PhoneStateListener Class
public class CustomPhoneStateListener extends PhoneStateListener {
Context context;
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("PHONEA", "IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("PHONEA", "OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("PHONEA", "RINGING");
Intent intent = new Intent(this.context, AudioService.class);
context.startService(intent);
break;
default:
break;
}
}
}
and this is my service
public class AudioService extends Service{
private static final String TAG = "PHONEA";
MediaPlayer player;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Log.d(TAG, "onCreate -> TODO");
}
}
My question is that every time I receive data in Broadcast receiver I create a new instance of the TelephonyManager. So when I view the logcat the first time I get "RINGING", the second time "RINGING" "RINGING" and so on. When should I create my telephonylistener in order to have only one instance?
Regards,
Nicos
You are getting call on your receiver(Asssuming you are listening for PhoneState) every time the phone state change.
You should put some check in your receiver and instatiate the TelephonyManager for the first time only.