androniennn
androniennn

Reputation: 3137

How to take a variable's value in the end of the code to use it in the begin of it

I have to post the code to understand what i want to say:

public void répondre()
    {
         mt = MediaPlayer.create(context,R.raw.ringtone);
        mt.setVolume(5,5);
      mt.start();
      mt.setLooping(true);
      //startActivity((new Intent(Intent.ACTION_ANSWER)));
        AlertDialog.Builder ad = new AlertDialog.Builder(context);
        ad.setTitle("Appel en cours...");
        ad.setMessage("Voulez vous répondre à cet appel?");
        ad.setPositiveButton("Oui", 
                             new OnClickListener() {
                               public void onClick(DialogInterface dialog, 
                                                   int arg1) {
                                   mt.stop();
                                SipAudioCall incomingCall = null;
                             try {

                                     SipAudioCall.Listener listener = new SipAudioCall.Listener() {
                                        @Override
                                        public void onRinging(SipAudioCall call, SipProfile caller) {
                                            try {

                                                call.answerCall(30);
                                            } catch (Exception e) {
                                                Log.d("Call not answered","Call not answered",e);
                                            }
                                        }

                                    };

                                    SIPCommunicator wtActivity = (SIPCommunicator) context;

                                    incomingCall = wtActivity.manager.takeAudioCall(intent, listener);
                                    incomingCall.answerCall(30);
                                    incomingCall.startAudio();
                                    incomingCall.setSpeakerMode(true);
                                    if(incomingCall.isMuted()) {
                                        incomingCall.toggleMute();
                                    }

                                    wtActivity.call = incomingCall;
                                 String useName = wtActivity.call.getPeerProfile().getDisplayName();
                                    wtActivity.updateStatus("Vous êtes en communication avec " + useName);



                             } catch (Exception e) {
                                if (incomingCall != null) {
                                    incomingCall.close();
                                }
                            }
                        }

                 });

I want to show the caller's name(useName variable) in the AlertDialog. If i put this line(String useName = wtActivity.call.getPeerProfile().getDisplayName();) before MediaPlayerand make ad.setTitle("Appel en cours"+useName); the variable useName is null ! So how doing this ? Thank you very much.

Upvotes: 0

Views: 143

Answers (1)

Zelimir
Zelimir

Reputation: 11028

Fortunatelly, we do not need to break causality as indicated in the question title. Thing is (and that is not something so uncommon with phone applications handling) that in the moment when you get indication about incoming call, caller name is not ready. It is because it takes some time until callers number/uri has to be searched for in the contacts.

So, in order to handle this issue I would suggest that you do one of the following:

  • indicate incoming call via AlertDialog, but present just caller number/URI. Start Handler and second later update the dialog so it displays name.
  • when call arrives, start handler and execute your code just second later. Hopefully at that moment Name will be available.

So, in any case you need to update your AlertDialog some safe time after incoming call is indicated.

Upvotes: 2

Related Questions