In my android application, I have created a BroadcastReceiver
that detects incoming call; my code is running very well. If there is an incoming call (EXTRA_STATE_RINGING
), I can see my incommingNumber in the logcat, also when the user answered the call (EXTRA_STATE_OFFHOOK
)
- I used
shared preferences
to store incoming number (String) in ringing state, then get it in Off HOOK state.
This is my code, it works perfectly:
public class IncomingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String incomingNumber = null ;
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING))
{
// Ringing state
// Phone number
incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("test2", incomingNumber);
SharedPreferences myPrefs = context.getSharedPreferences("myPrefs",Context.MODE_WORLD_WRITEABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("Incomingnumber", incomingNumber);
//Not forgot to commit.
prefsEditor.commit();
}
else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
//get the incoming number
SharedPreferences myPrefs = context.getSharedPreferences("myPrefs",Context.MODE_WORLD_READABLE);
String incomNumber = myPrefs.getString("Incomingnumber", incomingNumber);
// This code will execute when the call is answered
Log.i("test2", incomNumber);
Toast.makeText(context,incomNumber, Toast.LENGTH_LONG).show();
}
BUT:
My problem that my code can display a toast when the user just Accept
the incoming call.
So, I need to know how I can detect the end of an answered the incoming call to do something else (display an alert dialog or launch an activity)