1

i want to make some functionality like true caller that when a call comes gets missed by the user then it should show some message or dialog box this is my broadcast reciever class

public class MyReceiver extends BroadcastReceiver{
  
    private static int lastState = TelephonyManager.CALL_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static String savedNumber;


    protected void onIncomingCallStarted(Context ctx, String number, Date start) {
        Log.d("onIncomingCallStarted","no"+number);
    }
    protected void onOutgoingCallStarted(Context ctx, String number, Date start) {
        Log.d("onOutgoingCallStarted","no"+number);
    }
    protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end) {
        Log.d("onIncomingCallEnded","no"+number);
    }
    protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end) {
        Log.d("onOutgoingCallEnded","no"+number);
    }
    protected void onMissedCall(Context ctx, String number, Date start) {
        Log.d("onMissedCall","no"+number);
    }
    
    public void onCallStateChanged(Context context, int state, String number) {
        if(lastState == state){
            return;
        }
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                isIncoming = true;
                callStartTime = new Date();
                savedNumber = number;
                onIncomingCallStarted(context, number, callStartTime);
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                if(lastState != TelephonyManager.CALL_STATE_RINGING){
                    isIncoming = false;
                    callStartTime = new Date();
                    onOutgoingCallStarted(context, savedNumber, callStartTime);
                }
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                if(lastState == TelephonyManager.CALL_STATE_RINGING){
                    
                    onMissedCall(context, savedNumber, callStartTime);
                }
                else if(isIncoming){
                    onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
                }
                else{
                    onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
                }
                break;
        }
        lastState = state;
    }
    

    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);
        }
    }
} 

problem i'm facing is that, This app is working fine when its in foreground but as its gets killed broadcast receiver is not receiving and not giving any message i know there are some changes for version's>oreo but i don't no how to tackle this problem pls hlp me out. this my Menifest.xml:

 <receiver
        android:name=".Recievers.MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter android:priority="1000">
            <action android:name="android.intent.action.PHONE_STATE"/>

        </intent-filter>
    </receiver>
yash gupta
  • 47
  • 1
  • 7

1 Answers1

0
  1. This happens, may be you are debugging the app. When you close the app while debugging, it force stops. Try to manually run the app & test it again. If its not work,
  2. Implement Broadcast Receiver as a service. But it is not a good practice.
  3. Also check you have granted all the call permissions. Include all required permissions in Manifest. Here is all the permissions you needed (Note: PROCESS_OUTGOING_CALLS is now deprecated. Don't know your Target or min SDK, So I included).
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
Dinith Rukshan Kumara
  • 638
  • 2
  • 10
  • 19
  • I've given all these permission s in my manifest – yash gupta Jan 21 '21 at 10:32
  • Go to Settings -> apps -> "app name" -> permissions & check you have granted all those permissions. Refer this to implement receiver as a service: https://stackoverflow.com/questions/22257310/android-keep-broadcastreceiver-in-background – Dinith Rukshan Kumara Jan 21 '21 at 11:10
  • yes bro i'vb given all the permisions required i dont no that where i'm going wrong when app get's killed then this broadcast receiver is not working and or not giving any respose – yash gupta Jan 22 '21 at 05:08