kamal
kamal

Reputation: 2398

Incoming Call Receiver is not Cancelling the toast

I have an IncomingCallReceiver which extends BroadcastReciever.

Inside onReceive I want show some information using Toast till the user receives or rejects the call.

When the phone ringing I am showing toast using Loops.

When the user receive the call or reject the call I am cancelling the Toast.

But Toast does not get cancelled.

 public class IncommingCallReceiver extends BroadcastReceiver
         {

            Context context;
        static Toast toast;

            @Override
        public void onReceive(Context mContext, Intent intent)
         {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        TextView  tv=new TextView(mContext);
        tv.setBackgroundColor(color.background_light);
        Log.i("On Recieve"," ");
        //Toast toast=new Toast(mContext);
        if(state==null)
            return;
        if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
        {
            for(int i=0;i<7;i++)
            {
               toast=   Toast.makeText(mContext, "Ringing",Toast.LENGTH_LONG);
               toast.show();
            }


        }
         if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
         {
            // Toast.makeText(mContext, "Recieved", Toast.LENGTH_LONG).show();
             toast.cancel();

         }
        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
        {
            //Toast.makeText(mContext, "IDLE", Toast.LENGTH_LONG).show();
             toast.cancel();
        }






    }
 }

So how to Cancel the toast when user receives or reject the incoming call?

Upvotes: 0

Views: 358

Answers (1)

Oleg Vaskevich
Oleg Vaskevich

Reputation: 12682

The problem is that you are creating several toasts in a row - when one toast finishes the rest are shown sequentially. You are essentially creating 7 different Toast objects but only keeping a reference to the last one.

What you need to do is use one Toast; instead of Toast.LENGTH_LONG, use a different value. Then you should be able to call cancel().

Upvotes: 1

Related Questions