在BroadcastRecevier中未调用onRecevie方法

时间:2019-07-01 08:10:31

标签: android

在这里,在OutgoingReceiver中,当调用该类时,其内部的onReceive方法未调用。        但是当我调用OutgoingReciver类时,它只是在调用其构造函数。无法调用其中的方法。

可以建立呼出电话的连接。

如何调用类myListener,以便它可以告知调用处于哪种状态,如onCalling,onCallFounded。

其清单文件为:

 <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> 

     <receiver android:name=".OutgoingReceiver">
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

OutgoingRecevier类:-

        import android.content.BroadcastReceiver;
        import android.content.Context;
        import android.content.Intent;
        import android.net.sip.SipAudioCall;
        import android.util.Log;

       public class OutgoingReceiver extends BroadcastReceiver 
       {
        public OutgoingReceiver() 
        {   
           Log.d("outgoing","in outgoing listener");

           //this is been called
        }

     // but not invoking this onRecevie method. Were i am wrong??

      @Override
       public void onReceive(Context context, Intent intent) 
       { 
     Log.d("onRecevice","hi");

     String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

     SipAudioCall.Listener listener = new myListener(context);

   }
 }

myListener类:-

    import android.content.Context;
    import android.media.MediaPlayer;
    import android.net.sip.SipAudioCall;
    import android.net.sip.SipException;
    import android.net.sip.SipProfile;
    import android.util.Log;

    class myListener extends SipAudioCall.Listener {
private Context context;

public myListener(Context context)
{
    Log.d("mylistener","i am in");
    this.context = context;
}

@Override
public void onRinging(SipAudioCall call, SipProfile caller) {
    try {
        Log.d("inRinging","in");
        call.answerCall(30);
    } catch (SipException e) {
        e.printStackTrace();
    }
}

@Override
public void onReadyToCall(SipAudioCall call) {
    Log.d("ReadyCall","IncomingCallReceiver.java onReadyToCall : " + call.toString());
}

@Override
public void onCalling(SipAudioCall call) {
    Log.d("Calling","IncomingCallReceiver.java onCalling : " + call.toString());
}

@Override
public void onRingingBack(SipAudioCall call) {
    Log.d("RingingBack","IncomingCallReceiver.java onRingingBack : " + call.toString());
}

@Override
public void onCallEstablished(SipAudioCall call) {
    Log.d("CallEstablished","IncomingCallReceiver.java onCallEstablished : " + call.toString());
    if (call.isInCall()) {
        Log.d("CallEstablished","IncomingCallReceiver.java isInCall : " + call.toString());

    }
    if (call.isOnHold()) { 
        Log.d("CallEstablished","IncomingCallReceiver.java isOnHold : " + call.toString());
    }
    if (call.isMuted()) {
        Log.d("CallEstablished","IncomingCallReceiver.java isMuted : " + call.toString());
    }

    call.startAudio();
}

@Override
public void onCallEnded(SipAudioCall call) {
    Log.d("CallEnded","IncomingCallReceiver.java onCallEnded : " + call.toString());
}

@Override
public void onCallBusy(SipAudioCall call) {
    Log.d("CallBusy","IncomingCallReceiver.java onCallBusy : " + call.toString());
}

@Override
public void onCallHeld(SipAudioCall call) {
    Log.d("CallHeld","IncomingCallReceiver.java onCallHeld : " + call.toString());
}

@Override
public void onError(SipAudioCall call, int errorCode, String errorMessage) {
    Log.d("CallError","IncomingCallReceiver.java IncomingCallReceiver.java onError : " + call.toString() + "; errorCode: " + errorCode + "; errorMessage: " + errorMessage);
}

@Override
public void onChanged(SipAudioCall call) {
    Log.d("CallChanged","IncomingCallReceiver.java onReadyToCall : " + call.toString());
}

}

主要活动:-

           .
           .   //code
           .

          if(TelephonyManager.CALL_STATE_OFFHOOK == state) 

           {

           Log.i("endcallListener hook", "OFFHOOK");

          IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);

         //called here

            BroadcastReceiver br = new OutgoingReceiver();

            getApplicationContext().registerReceiver(br,intentFilter);

        }
       .
       .   //code
       .

输出:-

       I/endcallListener hook: OFFHOOK
       D/outgoing: in outgoing listener
       //terminated

提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果要使用TelephonyManager捕获外拨电话,请执行以下操作:

allocs/ops