4

I am trying to log the phone number whenever I get an incoming call using BroadcastReceiver . However, I do not get any log when I dial to my number. This is the code:

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abc.broadcast_phone" >

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >

        <receiver android:name=".MyPhoneReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" >
                </action>
            </intent-filter>
        </receiver>
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

MainActivity.java

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent("tuet");
        sendBroadcast(intent);
    }
}

MyPhoneReceiver.java

    public class MyPhoneReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null)
        {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);

            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                String phoneNumber = extras
                        .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Log.e("DEBUG", phoneNumber);
            }
        }
    }
}

I am compiling using SDK 23 and target is also Marshmallow, should I be using run time permission in this case? Or am I missing something?

EDIT-1

Seems like the problem is only associated with Marshmallow onwards, when I downgraded the target sdk version, this is working fine : targetSdkVersion 22 .

So, how to tweak this to work in SDK23+ onwards?

OBX
  • 6,044
  • 7
  • 33
  • 77

4 Answers4

2

Per the onCallStateChanged() documentation:

incoming call phone number. If application does not have READ_PHONE_STATE permission, an empty string will be passed as an argument.

And READ_PHONE_STATE is a dangerous permission that must be requested at runtime when you target API 23 or higher as per the Marshmallow Behavior Changes

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
1

It is a permission issue. In Marshmallow you deal with permissions at runtime if your app targets SDK 23. See https://developer.android.com/training/permissions/requesting.html. You can test this, by manually enabling all permissions your app needs in the System Settings, then your app will work.

lionscribe
  • 3,413
  • 1
  • 16
  • 21
0

Try this snippet in your onReceive method :

@Override
    public void onReceive(final Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                Log.e("DEBUG", incomingNumber);
            }
        },PhoneStateListener.LISTEN_CALL_STATE);
    }

Ref : https://stackoverflow.com/a/13154533/2724418

Community
  • 1
  • 1
YFeizi
  • 1,498
  • 3
  • 28
  • 50
0

Try this :

In the Manifest :

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

In PhoneCallReceiver:

public void onReceive(Context context, Intent intent) {
    try {
        if (intent.getAction().equals("android.intent.action.PHONE_STATE"))
        {
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
            {
                 TelephonyManager tmgr = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);




                Bundle bundle = intent.getExtras();
                String phoneNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Log.d("INCOMING", phoneNumber);

            }
        }
    } catch (Exception e) {
        Log.e("Phone Receive Error", " " + e);
    }
}
Smit Davda
  • 638
  • 5
  • 15
  • For Marshmallow onwards you need to grant the android.permission.READ_PHONE_STATE permission. you can test it by granting it from app settings. It is working for me – Smit Davda Aug 10 '16 at 05:45