Android:无法检测拨出号码

时间:2011-12-07 21:36:09

标签: android broadcastreceiver

当我尝试从设备发出传出呼叫时,我无法检测到什么是传出号码,它总是给我空指针异常。我想检测拨出的电话号码。请指教,我尝试搜索解决方案,但无法使其正常工作。 Nullpointer异常即将出现在这一行 - > Log.i("OutgoingCallReceiver",phonenumber);

12-07 16:10:50.576: ERROR/AndroidRuntime(3145): Caused by: java.lang.NullPointerException: println needs a message
12-07 16:10:50.576: ERROR/AndroidRuntime(3145):     at android.util.Log.println_native(Native Method)
12-07 16:10:50.576: ERROR/AndroidRuntime(3145):     at android.util.Log.i(Log.java:143)
12-07 16:10:50.576: ERROR/AndroidRuntime(3145):     at com.cwc.OutgoingCallReceiver.onReceive(OutgoingCallReceiver.java:27)
12-07 16:10:50.576: ERROR/AndroidRuntime(3145):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
12-07 16:10:50.576: ERROR/AndroidRuntime(3145):     ... 10 more

这是我的班级文件

public class OutgoingCallReceiver extends BroadcastReceiver {


// private static final String INTENT_PHONE_NUMBER = "android.intent.extra.PHONE_NUMBER";

    @Override
    public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();

            if(null == bundle)
                    return;


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

            Log.i("OutgoingCallReceiver",phonenumber);
            Log.i("OutgoingCallReceiver",bundle.toString());

            String info = "Detect Calls sample application\nOutgoing number: " + phonenumber;

            Toast.makeText(context, info, Toast.LENGTH_LONG).show();
    }
}

我在清单文件中有以下条目

    <receiver android:name=".OutgoingCallReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

</application>

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

2 个答案:

答案 0 :(得分:1)

解决了,我错过了“android.permission.PROCESS_OUTGOING_CALLS”用户权限。 它现在有效,这是我更新的清单文件。

答案 1 :(得分:1)

onReceive方法中,在广播接收器中,只需使用捆绑包中的值即可   
Intent.EXTRA_PHONE_NUMBER

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

就是这样!!

在清单

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

在BroadCast接收器下:

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