1

i try catch a call from standard dialpad using that code:

        <action android:name="android.intent.action.CALL" />
        <data android:scheme="tel" />
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.CALL_PRIVILEGED" />
   </intent-filter>


</activity>

Everything fine, when user dial from standard phone dialpad, my app opened. But i don't find solution, how i can get a phone number, which user was dialed. That code inside PhonePadActivity activity onCreate block:

Intent intent = getIntent();

String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();

gives me a null finally :(

tried to using brodacast receiver:

in manifest:

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

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

this is class DialBroadcastReceiver:

package com.myapps.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class DialBroadcastReceiver extends BroadcastReceiver {
    private static final String THIS_FILE = "PhonePadActivity";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.e(THIS_FILE,"In onReceive()");

        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
             String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

             Log.e(THIS_FILE,"Number is: "+number);

        }
    }

}

but logs nod fired, when user press dial

user170317
  • 1,152
  • 2
  • 11
  • 22

4 Answers4

3

This works for me:

In Manifest:

<intent-filter>
    <action android:name="android.intent.action.CALL"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <action android:name="android.intent.action.CALL_PRIVILEGED"/>
    <data android:scheme="tel"/>
</intent-filter>

In Activity:

String inputURI = this.getIntent().getDataString();
if (inputURI != null) {
    Uri uri = Uri.parse(Uri.decode(inputURI));
    if (uri.getScheme().equals("tel")) {
        String calledNumber = uri.toString();
    }
}
Jakub Kinst
  • 357
  • 2
  • 9
2

You are getting the number with incorrect code. Replace:

Intent intent = getIntent();

String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();

With:

Uri data = getIntent().getData(); 
if (data != null && ("tel".equals(data.getScheme()))) { 
    String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this); 
    if (number != null) { 
      Toast.makeText(this, "Call was made to-->>" + number, 5000).show();
    } 
}
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
1
  • Add <intent-filter android:priority="9999"> to the intent-filter declaration in the manifest, to make sure you're first in line
  • Remove the com.myapps.android-part from the android:name property of the receiver declaration in the manifest (i.e. it should be: android:name=".DialBroadcastReceiver")
Nick
  • 3,504
  • 2
  • 39
  • 78
  • hi. i can't verify if priority working well, bcs i'm in list first as my app started from C ;) but thnx for advanced topic – user170317 Dec 30 '12 at 12:22
0

Register a BroadcastReceiver like this in Manifest file:

<!-- DIAL Receiver -->
<receiver
    android:exported="true"
    android:name="receivers.DialBroadcastReceiver" >
    <intent-filter >
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    </intent-filter>
</receiver>

You need Permission for NEW_OUTGOING_CALL :

Eventually you can Receive the broadcast and retrieve the dialed number like this:

public class DialBroadcastReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {

    Log.v("DileBroadCastReceiver","In onReceive()");

    if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
      String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
      Log.v("DialBroadcast Receiver","Number is: "+number);
    }
  }
}
Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
Muhammad Noman
  • 1,842
  • 19
  • 14