I am new to Android(3 months old). I want to read an incoming call and display it through MainActivity. I have extended PhoneStateListener and am able to detect the incoming call/number. Now my question is how do I display this number in the front end. Please help. Thanks a lot.
Asked
Active
Viewed 131 times
-1
-
Have you tried anything? If yes please share your code. – pa1.Shetty Apr 23 '19 at 10:54
1 Answers
0
you can use broadcast receiver to show incoming or outgoing calls :- create a class CallReceiver :-
public class CallReceiver extends BroadcastReceiver {
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
} else {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
state = TelephonyManager.CALL_STATE_IDLE;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
state = TelephonyManager.CALL_STATE_OFFHOOK;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
}
onCallStateChanged(context, state, number, intent);
}
}
public void onCallStateChanged(Context context, int state, String number, Intent intent) {
if (lastState == state) {
//No change, debounce extras
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
onIncomingCallStarted(context, number, callStartTime, intent);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing done on them
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, number, callStartTime, intent);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//Went to idle- this is the end of a call. What type depends on previous state(s)
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
//Ring but no pickup- a miss
onMissedCall(context, number, callStartTime, intent);
} else if (isIncoming) {
onIncomingCallEnded(context, number, callStartTime, new Date(), intent);
} else {
onOutgoingCallEnded(context, number, callStartTime, new Date(), intent);
}
break;
}
lastState = state;
Intent intent1 = new Intent("CallApp");
context.sendBroadcast(intent1);
}
protected void onIncomingCallStarted(Context ctx, String number, Date start, Intent intent) {
Toast.makeText(ctx, "calling from " + number, Toast.LENGTH_SHORT).show();
}
protected void onOutgoingCallStarted(Context ctx, String number, Date start, Intent intent) {
Toast.makeText(ctx, "calling to " + number, Toast.LENGTH_SHORT).show();
}
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end, Intent intent) {
Toast.makeText(ctx, "calling from " + number + " ended ", Toast.LENGTH_SHORT).show();
}
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end, Intent intent) {
Toast.makeText(ctx, "calling to " + number + " ended ", Toast.LENGTH_SHORT).show();
}
protected void onMissedCall(Context ctx, String number, Date start, Intent intent) {
Toast.makeText(ctx, "missed call from " + number + " sim ", Toast.LENGTH_SHORT).show();
}
}
or register your app in manifest :-
<receiver android:name=".broadcaestReceiver.CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>

Sandeep Malik
- 1,972
- 1
- 8
- 17