Jagdish
Jagdish

Reputation: 241

Android: unable to detect outgoing number

I am unable to detect what is outgoing number, its always giving me null pointer exception when i try to place outgoing call from device. I want to detect outgoing telephone number. Please advise, I tried searching solutions, but unable to make it work. Nullpointer exception is coming on this line -> 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

Here is my class file

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();
    }
}

I have following entry in manifest file

    <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" />

Upvotes: 3

Views: 2544

Answers (2)

Exceptional
Exceptional

Reputation: 3004

In the onReceive method,in a broadcast receiver, just get the value from the bundle using
Intent.EXTRA_PHONE_NUMBER

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

That's it!!

In manifest

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

Under the BroadCast Receiver:

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

Upvotes: 1

Jagdish
Jagdish

Reputation: 241

Solved, I was missing "android.permission.PROCESS_OUTGOING_CALLS" user permission. It works now, here is my updated manifest file.

Upvotes: 1

Related Questions