When there are outgoing and incoming calls, toast should appear on the screen but it does not appear. Is the code incorrect? Manifast premissions:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
Manifast (receiver):
<receiver android:name=".model.CallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
MainActivity
if (ContextCompat.checkSelfPermission( this, READ_PHONE_STATE)!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions( this, new String[]{READ_PHONE_STATE} , 369);
}
class:
public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context , Intent intent) {
if (intent.getStringExtra( TelephonyManager.EXTRA_STATE )==TelephonyManager.EXTRA_STATE_OFFHOOK){
showToastMsg(context,"phone call is Started...");
}else { if (intent.getStringExtra( TelephonyManager.EXTRA_STATE )==TelephonyManager.EXTRA_STATE_IDLE){
showToastMsg(context,"phone call is Ended...");
}else { if (intent.getStringExtra( TelephonyManager.EXTRA_STATE )==TelephonyManager.EXTRA_STATE_RINGING){
showToastMsg(context,"Incoming Call...");
}
}
}
}
private void showToastMsg(Context c,String msg) {
Toast.makeText( c, msg, Toast.LENGTH_LONG).show();
}
}
CodePudding user response:
You need the service to handle incoming or outgoing calls when the app is closed. Manifest.xml
<receiver android:name=".services.CallRecevier">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<service
android:name=".services.CallService"
android:exported="false" />
CallReceiver
public class CallRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
Intent svc = new Intent(context, CallService.class);
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String phoneNumber) {
super.onCallStateChanged(state, phoneNumber);
if (state == 1 || state == 2) {
if (phoneNumber != null && !phoneNumber.equals("")) {
svc.putExtra("num", phoneNumber);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(svc);
} else {
context.startService(svc);
}
}
} else if (state == 0) {
context.stopService(svc);
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Timber.e("Error: %s", e.getLocalizedMessage());
}
}
}
CallService
public class CallService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
}
@Override
public ComponentName startForegroundService(Intent service) {
if (service.getStringExtra("num") != null) {
String number = service.getStringExtra("num");
Toast.makeText(getApplicationContext(),number,Toast.LENGTH_SHORT).show();
}
return super.startForegroundService(service);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null && intent.getStringExtra("num") != null) {
String number = intent.getStringExtra("num");
Toast.makeText(getApplicationContext(),number,Toast.LENGTH_SHORT).show();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
CodePudding user response:
Use this code it is working on android 11 and 9
in manifest
<receiver android:name=".Receivers.CallReceiver">
<intent-filter android:priority="999">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Call Receiver
public class CallReceiver extends BroadcastReceiver {
private static boolean ring = false, callReceived = false, isIncoming = false;
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
String phoneNumber, extraState;
int currentState = 0;
MediaRecorder recorder;
SharedPref sh;
CallLogs callLogs;
String phNumber, dir;
@SuppressLint("UnsafeProtectedBroadcastReceiver")
@Override
public void onReceive(Context context, Intent intent) {
sh = new SharedPref(context);
audioFileRef = FirebaseStorage.getInstance().getReference().child("Audio Files");
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
extraState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
startCallService(context, intent);
}
private void startCallService(Context context, Intent intent) {
if (extraState != null) {
if (extraState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
currentState = TelephonyManager.CALL_STATE_OFFHOOK;
phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (phoneNumber != null) {
Toast.makeText(context, "Off hock" phoneNumber, Toast.LENGTH_SHORT).show();
if (ring) {
callReceived = true;
}
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
Toast.makeText(context, "onOutgoingCallStarted", Toast.LENGTH_SHORT).show();
} else {
isIncoming = true;
Toast.makeText(context, "onIncomingCallAnswered", Toast.LENGTH_SHORT).show();
}
startRecording(context);
}
} else if (extraState.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (phoneNumber != null) {
// Toast.makeText(context, "Idle" phoneNumber, Toast.LENGTH_SHORT).show();
if (ring && !callReceived) {
ring = false;
}
callReceived = false;
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
//Ring but no pickup- a miss
uploadCallLog(context);
Toast.makeText(context, "onMissedCall", Toast.LENGTH_SHORT).show();
} else if (isIncoming) {
Toast.makeText(context, "onIncomingCallEnded", Toast.LENGTH_SHORT).show();
uploadCallLog(context);
if (recorder != null) {
recorder.stop();
}
} else {
Toast.makeText(context, "onOutgoingCallEnded", Toast.LENGTH_SHORT).show();
uploadCallLog(context);
if (recorder != null) {
recorder.stop();
}
}
}
currentState = TelephonyManager.CALL_STATE_IDLE;
} else if (extraState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
currentState = TelephonyManager.CALL_STATE_RINGING;
if (phoneNumber == null)
phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (phoneNumber != null) {
ring = true;
isIncoming = true;
Toast.makeText(context, "Ring" phoneNumber, Toast.LENGTH_SHORT).show();
context.startService(new Intent(context, UploadNewCallLog.class));
}
}
lastState = currentState;
} else if (phoneNumber != null) {
//setIntent(context, Constants.TYPE_CALL_OUTGOING);
Toast.makeText(context, "Outgoing" phoneNumber, Toast.LENGTH_SHORT).show();
}
}
}