在android中错过了呼叫检测

 K_M_睡到自然醒cES_881 发布于 2022-12-09 12:39

我想开发一个未接来电探测器.通过广播接收器通知我从以下链接找到了代码

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

    }

}

上面的代码很好.但是当我从同一时间得到未接来电时,它会重复未接来电检测.我将如何获得祝酒,请帮助

1 个回答
  • 大家好,我现在已经得到了解决方案,它的工作正常,未接来电.

    这里我的类代码MyCallListener扩展了BroadcastReciever

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

    别忘了在清单文件中定义接收器

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

    在应用程序标签中

    2022-12-11 02:14 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有