【问题标题】:Incoming Call Receiver is not Cancelling the toast来电接听者未取消敬酒
【发布时间】:2013-01-04 07:25:22
【问题描述】:

我有一个IncomingCallReceiver,它扩展了BroadcastReciever

onReceive 内部,我想使用 Toast 显示一些信息,直到用户接听或拒绝来电。

当电话响起时,我正在使用 Loops 显示 toast。

当用户接到电话或拒绝来电时,我正在取消 Toast。

但是 Toast 并没有被取消。

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






    }
 }

那么当用户接听或拒接来电时如何取消toast呢?

【问题讨论】:

    标签: android broadcastreceiver toast


    【解决方案1】:

    问题是您连续创建了几个 toast - 当一个 toast 完成时,其余的按顺序显示。您实际上是在创建 7 个不同的 Toast 对象,但只保留对最后一个对象的引用。

    您需要做的是使用一个 Toast;而不是Toast.LENGTH_LONG,使用不同的值。那么你应该可以拨打cancel()

    【讨论】:

      猜你喜欢
      • 2014-01-16
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多