Android中的未接来电检测 [英] Missed call detection in android

查看:288
本文介绍了Android中的未接来电检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个未接来电检测器.通过广播接收器通知,我从以下

I want to develop a missed call detector .Which notify through broadcast reciver I found the code from the following link

public abstract class PhoneCallReceiver extends BroadcastReceiver {
        static CallStartEndDetector listener;

    @Override
    public void onReceive(Context context, Intent intent) {
        savedContext = context;
        if(listener == null){
            listener = new CallStartEndDetector();
        }


            TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
        telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    }



    public class CallStartEndDetector extends PhoneStateListener {
        int lastState = TelephonyManager.CALL_STATE_IDLE;
        boolean isIncoming;

        public PhonecallStartEndDetector() {}


        //Incoming call-   IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when hung up
        //Outgoing call-  from IDLE to OFFHOOK when dialed out, to IDLE when hunged up

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            if(lastState == state){
                //No change
                return;
            }
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    isIncoming = true;
                     //incoming call started
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    //Transition of ringing->offhook are pickups of incoming calls.  Nothing down on them
                    if(lastState != TelephonyManager.CALL_STATE_RINGING){
                        isIncoming = false;
                       //outgoing call started
                    }
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    //End of call(Idle).  The type depends on the previous state(s)
                    if(lastState == TelephonyManager.CALL_STATE_RINGING){
                       //toast here for missed call
                    }
                    else if(isIncoming){
                          //incoming call ended
                    }
                    else{
                       //outgoing call ended                                              
                    }
                    break;
            }
            lastState = state;
        }

    }

}

上面的代码可以正常工作.但是当我从相同的位置收到未接来电时,它会重复进行未接来电检测.我怎么只吃一次烤面包请帮助

The above code woking fine .But when i got the missed call from same no it repeat the missed call detection . How i will got the toast only once please help

推荐答案

大家好,我现在有了解决方案,它可以很好地处理未接来电.

Hi everyone I have got the solution now its working fine with missed call.

这是我的班级代码MyCallListenerBroadcastReciever

private static boolean ring=false;
private static boolean callReceived=false;
private String callerPhoneNumber;
private Context saveContext;

@Override
public void onReceive(Context mContext, Intent intent)
{
     saveContext=mContext;
   // Get the current Phone State


    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

    if(state==null){
      return;
    }

    Bundle bundle = intent.getExtras();
    number= bundle.getString("incoming_number");
    Calendar calendar=Calendar.getInstance();
    currentTimeStamp=calendar.getTimeInMillis();
    // If phone state "Rininging"
    if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    {           
              ring =true;
             // Get the Caller's Phone Number

      }



     // If incoming call is received
    if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
     {
            callReceived=true;
      }


     // If phone is Idle
    if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
    {
              // If phone was ringing(ring=true) and not received(callReceived=false) , then it is a missed call
               if(ring==true&&callReceived==false)
               {
                        Toast.makeText(mContext, "missed call : "+number, Toast.LENGTH_LONG).show();
                       //workingWithFunctions();
                        ring=false;
               }
               callReceived=false;
  }}

不要忘记在清单文件中定义接收方

dont forgot to define receiver in manifest file

 <receiver android:name="com.abc.broadcastrecivers.MyBroadcastReciver" />

在应用程序标记中

这篇关于Android中的未接来电检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆