如何获取正在进行的通话电话号码

时间:2012-02-28 07:53:56

标签: android broadcastreceiver phone-call

我使用'startActivity(callintent)'启动'Activity'并等待一段时间。然后我需要用'onReceive()'来停止呼叫。在这个'onReceive()'方法中,我如何获得现有的正在进行的电话号码?

@Override
public void onReceive(Context context, Intent intent) {
    String oldnumber = intent.getStringExtra(intent.ACTION_NEW_OUTGOING_CALL);
    String newphnumber="9999999999";
    if ((newphnumber1=null) &&(newphnumber!=oldnumber)) {
        String msg = "Intercepted outgoing call. Old :" + oldnumber + 
                     ", new :" + newPhNnumber;
        Toast.makeText(getApplicationContext(), "end call", Toast.LENGTH_SHORT).
            show();
        setResultData(newphnumber);
    }
}

此处oldnumber表示已激活的拨出号码(单个呼叫正在进行)。我怎么能得到它?

1 个答案:

答案 0 :(得分:3)

我们可以使用BroadcastReceiver轻松获取正在进行的电话号码。 在您的BroadcastReceiver类onReceive函数中,您必须编写如下代码,

if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER).toString();
            Log.w("", "Number---->"+number);
             Toast.makeText(context, "Intent Received", Toast.LENGTH_LONG).show();
}

并在您的Activity类中

MReciever mReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  
        mReceiver = new MReciever();
        IntentFilter filter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
        registerReceiver(mReceiver, filter);
    }

    protected void onDestroy() {
        unregisterReceiver(mReceiver);
    };

在Android清单文件中,您必须将接收方声明为

<receiver android:name=".YourReceiverClass" android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    </intent-filter>
    </receiver>

我认为这会对你有帮助。