你怎么收到的BroadcastReceiver呼出你怎么、呼出、BroadcastReceiver

2023-09-13 01:38:42 作者:烟的寂寞

我试图找出并传送到活动开始拨出电话后。我用ACTION_NEW_OUTGOING_CALL的意图过滤器。但是如何CSN我确定呼叫传出。我这样做的来电(如下图所示),但我什么都用EXTRA_STATE_RINGING代替。

 公共类OutgoingBroadcastReceiver扩展的BroadcastReceiver {

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    字符串状态= intent.getStringExtra(TelephonyManager.EXTRA_STATE);

      如果(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
      {


    意图I =新的意图(背景下,OutgoingCallScreenDisplay.class);

    i.putExtras(意向);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    context.startActivity(ⅰ);
      }
}
 

}

解决方案

 公共类OutgoingBroadcastReceiver扩展的BroadcastReceiver {

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    如果(意向。的getAction()。等于(意向。ACTION_NEW_OUTGOING_CALL)){
//如果调用(拨出)
    意图I =新的意图(背景下,OutgoingCallScreenDisplay.class);
    i.putExtras(意向);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(ⅰ);
      }
}

}
 

由于 ACTION_NEW_OUTGOING_CALL 是恒定的申报意向不是在 TelephonyManager 所以,当传出呼叫进而出现系统广播的的意图的这个常数,而若你真的想用Telephonymanager然后赶上拨出电话: -

  TelephonyManager TM =(TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
            tm.li​​sten(监听器,PhoneStateListener.LISTEN_CALL_STATE);
PhoneStateListener监听器=新PhoneStateListener(){
        @覆盖
        公共无效onCallStateChanged(INT状态,串incomingNumber){
            // TODO自动生成方法存根

            super.onCallStateChanged(州,incomingNumber);
            开关(州){
            案例TelephonyManager.CALL_STATE_IDLE:
                打破;
            案例TelephonyManager.CALL_STATE_OFFHOOK:
                如果(incomingNumber == NULL)
                               {
                                 //呼出
                                }
                             其他
                                {
                                 //来电
                                }
                打破;
            案例TelephonyManager.CALL_STATE_RINGING:

                      如果(incomingNumber == NULL)
                        {
                          //呼出
                        }
                       其他
                        {
                          //来电
                        }
                打破;
            }
        }

    };
 
Android之BroadcastReceiver

I am trying to identify and transfer to an activity after an outgoing call is initiated. I used ACTION_NEW_OUTGOING_CALL in the Intent filter. However how csn I identify that the call is outgoing. I did this for an incoming call (as seen below) but what can I use instead of"EXTRA_STATE_RINGING".

public class OutgoingBroadcastReceiver extends BroadcastReceiver {

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

      if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
      {


    Intent i = new Intent(context, OutgoingCallScreenDisplay.class);

    i.putExtras(intent);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    context.startActivity(i);
      }
}

}

解决方案

public class OutgoingBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (intent. getAction (). equals (Intent. ACTION_NEW_OUTGOING_CALL)) {
// If it is to call (outgoing)
    Intent i = new Intent(context, OutgoingCallScreenDisplay.class);
    i.putExtras(intent);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
      }
}

}

Because ACTION_NEW_OUTGOING_CALL is Constant declare in Intent not in TelephonyManager so when an outgoing call appear then system broadcast an Intent with this constant and if you really want to catch an outgoing call by using Telephonymanager then :-

TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);   
            tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
PhoneStateListener listener=new PhoneStateListener(){
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            // TODO Auto-generated method stub

            super.onCallStateChanged(state, incomingNumber);
            switch(state){
            case TelephonyManager.CALL_STATE_IDLE:
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                if(incomingNumber==null)
                               {
                                 //outgoing call
                                }
                             else
                                {
                                 //incoming call
                                }
                break;
            case TelephonyManager.CALL_STATE_RINGING:

                      if(incomingNumber==null)
                        {
                          //outgoing call
                        }
                       else
                        {
                          //incoming call
                        }
                break;
            }
        }

    };

 
精彩推荐
图片推荐