Search code examples
androidandroid-activitynumbersbroadcastreceiverphone-call

When pressed the call button, can't get number


When calling a number, the number don't display on Toast message. Here is my code:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

}

By the way when pressed call button, above application start running. And I give all permissions like this:

 <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <receiver android:name=".OutgoingCallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />

            <action android:name="android.intent.action.CALL_PRIVILEGED" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:scheme="tel" />

            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Solution

  • You have to register the receiver:

    registerReceiver(OutGoingCallReceiver, intentFilter);
    

    Don't forget to unregister it on shutdown.

    Also you may have to add Intent.CATEGORY_DEFAULT to your intent filter in Java code and manifest:

    Java:

    IntentFilter intentFilter = new IntentFilter(outgoing);
    intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
    

    AndroidManifest.xml:

    <receiver android:name=".OutgoingCallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
    

    EDIT:

    If you want your app not to intercept calls but just work as alternative dialer then you don't need NEW_OUTGOING_CALL action, instead you have to handle CALL_PIVILEGED action in your activity's onStart(), roughly like this:

    @Override
    public void onStart() {
        super.onStart();
        Intent intent = getIntent();
        if (intent == null)
            return;
    
        String action = intent.getAction();
        if (action.equals("android.intent.action.CALL_PRIVILEGED") || action.equals("android.intent.action.CALL")) {
            android.net.Uri contentUri = intent.getData();
            intent.setData(null);
            String outgoingno = contentUri.getSchemeSpecificPart().replaceAll("\\s","");
            Toast.makeText(context, "outgoingnum =" + outgoingno,Toast.LENGTH_LONG).show();
    }
    

    Also you may need to override onNewIntent as well.

    Note that on some phones you may need to hook on android.intent.action.CALL. Also on some phones (like HTC) this won't work completely because they have custom dialer apps which don't fire intents.