OBX
OBX

Reputation: 6114

Unable to detect incoming call number using BroadcastReceiver [ Marshmallow]

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?

Upvotes: 4

Views: 1585

Answers (4)

ianhanniballake
ianhanniballake

Reputation: 199795

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

Upvotes: 2

lionscribe
lionscribe

Reputation: 3513

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.

Upvotes: 1

Smit Davda
Smit Davda

Reputation: 638

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

Upvotes: 0

YFeizi
YFeizi

Reputation: 1528

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

Upvotes: 0

Related Questions