Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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



    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
172 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...