how to find outgoing number in Telephony manager

a 夏天 提交于 2019-12-07 04:43:39

问题


I am using this:

public void onCallStateChanged(int state, String incomingNumber)

which is listening to:

telephonyManager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);

I want to know both outgoing and incoming calls but for now I only get incoming calls (when state changes is ringing). Can anyone tell me when can I detect outgoing call and its end

Also is there a way to simulate outgoing calls in Eclipse emulator. was able to do that for incoming calls via emulator control in eclipse.


回答1:


Use a broadcast listener with an intent android.intent.action.NEW_OUTGOING_CALL string parametrer for the IntentFilter and don't forget to give permission in AndroidMenifest to PROCESS_OUTGOING_CALLS. This will work. Whenever there is an outgoing call a toast message will be shown. Code is below.

public static final String outgoing = "android.intent.action.NEW_OUTGOING_CALL" ;
IntentFilter intentFilter = new IntentFilter(outgoing);
BroadcastReceiver OutGoingCallReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        // TODO Auto-generated method stub
        String outgoingno = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Toast.makeText(context, "outgoingnum =" + outgoingno,Toast.LENGTH_LONG).show();
    }
};
registerReceiver(brForOutgoingCall, intentFilter);



回答2:


Create a new class, let say MyPhoneReceiver, extends it from BroadcastReceiver, and implement onReceive method.

public class MyPhoneReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent){

        String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

    }
}

In another class, let say, MainActivity.class inside onCreate method. for example.

    IntentFilter filter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL");
    MyPhoneReceiver myPhoneReceiver = new MyPhoneReceiver();
    registerReceiver(myPhoneReceiver,filter);

In the AndroidManifest.xml

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

and also in the AndroidManifest.xml, add:

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


来源:https://stackoverflow.com/questions/6611197/how-to-find-outgoing-number-in-telephony-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!