从broadcastReceiver android中检索传出号码

时间:2014-03-13 08:03:08

标签: android broadcastreceiver

我正在尝试为拨出电话创建自定义屏幕。我希望得到外出号码。

我正在查看此链接,该链接使用单独的广播接收器进行传入和传出。

http://goo.gl/YUD1b5

但是没有用。我打电话时收到错误消息

java.lang.NullPointerException: println needs a message

我有一个有清单的广播接收器:

     <receiver
        android:name="com.honey.ringer.PhoneListenerBroad"
        android:priority="999"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

当我呼叫时,广播接收器被呼叫,但我无法检索外出呼叫电话号码。我不知道该怎么做:

这是广播接收器文件:

public void onReceive(Context context, Intent intent) 
 {
    c = context;

    try
    {
        TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
        tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    }
    catch (Exception e) 
    {
        Log.e("Phone Receive Error", " " + e);
    }

}

private class MyPhoneStateListener extends PhoneStateListener
{
    public void onCallStateChanged(final int state, final String incomingNumber) 
    {
        Handler callActionHandler = new Handler();

        Runnable runRingingActivity = new Runnable() 
        {
            @Override
            public void run() 
            {
                Intent intentPhoneCall = new Intent(c, AcceptMakeCall.class);
                intentPhoneCall.putExtra("incomingnumber", incomingNumber);
                intentPhoneCall.putExtra("state", state);
                intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                c.startActivity(intentPhoneCall);
            }
        };

        if (state == 1)
        {
            callActionHandler.postDelayed(runRingingActivity, 100);
        }

        if (state == 0) 
        {
            callActionHandler.removeCallbacks(runRingingActivity);
        }

        if (state == 2)
        {
            /*callActionHandler.postDelayed(runRingingActivity, 100);*/


        }
    }
}

有人可以帮我拿出号码吗?

1 个答案:

答案 0 :(得分:2)

in your onRecieve


if (intent.getAction()
            .equals("android.intent.action.NEW_OUTGOING_CALL")) {

String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
Log.e("Number=",number);

}


and using permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
This worked for me.Hope it works for you also