Unable to determine incoming call number using BroadcastReceiver [Marshmallow]

I am trying to record a phone number when I receive an incoming call using BroadcastReceiver. However, I do not receive any logwhen I dial the 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 the target is also Marshmallow, should I use runtime resolution in this case? Or am I missing something?

EDIT-1

It seems that the problem is only with Marshmallow, when I lowered the target sdk version, it works fine: targetSdkVersion 22.

So how to configure this to work in SDK23 + and beyond?

+4
source share
4

onCallStateChanged():

. READ_PHONE_STATE, .

READ_PHONE_STATE , runtime, API 23 Marshmallow

+2
+1

onReceive:

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

: fooobar.com/questions/441203/...

0

:

:

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

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

Source: https://habr.com/ru/post/1650753/


All Articles