How to get alert dialog only after disconnect call

2020-05-10 06:22发布

问题:

In my app i am doing one thing that,when the call is disconnected by caller or receiver,one alert dialog should appear with cellphone number.it all works fine but issue is alert dialog is also appearing when i am getting call,but i only want only after disconnection,i don't know what is mistake i am making following is my code..can any one help?

     public class MyCallReceiver extends BroadcastReceiver {

private String incomingNumber;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if  (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)) {
        // This code will execute when the phone has an incoming call

        // get the phone number 

        incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        Intent i = new Intent(context, Disp_Alert_dialog.class); 
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("Number", incomingNumber);
        context.startActivity(i);
        Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();


       /* String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();*/

    } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_IDLE)
            || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                    TelephonyManager.EXTRA_STATE_OFFHOOK)) {
        // This code will execute when the call is disconnected



    }
}

回答1:

You have written your code in wrong place. This code

Intent i = new Intent(context, Disp_Alert_dialog.class);   
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("Number", incomingNumber); 
context.startActivity(i);

will be in else..if condition instead of if condition.



回答2:

You condition is wrong. You are using else block of

if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)

It will run whenever call state changes to 'Ideal' or 'off hook' (in outgoing call - when dialed and call is disconnected, in incoming call - when call is picked up and call is disconnected).

If you want to show dialogue only while disconnecting call, use

if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDEAL)

and write code in if block

As for number, you cannot get number that way as it is only valid for ringing state. A work around would be using a ContentObserver on CallLog. Its onChange() will be called whenever there is a change in call log. You can then take number from call log. But you will need to have a part of your application active for this. Use in a Service may be.

Note- In some devices message also comes in call log data. So check if last entry in call log in of call type or not before showing your dialogue

This may help you in using content observer