I need to catch an incoming call, and depending on its number, handle it accordingly. The project so far consists of an activity where only permissions are requested, a manifest and a receiver. Activity code MainActivity.java:
package roninapps.callblocker; import android.Manifest; import android.content.pm.PackageManager; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity { private static final int REQUEST_CODE_PARAMS = 1; private static final int REQUEST_CODE_READ_PHONE_STATE = 1; private static boolean READ_PHONE_STATE_GRANTED = false; private static final int REQUEST_CODE_READ_PHONE_NUMBERS = 2; private static boolean READ_PHONE_NUMBERS_GRANTED = false; private static boolean READ_CALL_LOG_GRANTED = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int hasReadPhoneStatePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE); int hasReadPhoneNumbersPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_NUMBERS); int hasReadCallLogPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG); if (hasReadPhoneStatePermission == PackageManager.PERMISSION_GRANTED) { Log.i("permissions", "phone state granted"); READ_PHONE_STATE_GRANTED = true; } if (hasReadPhoneNumbersPermission == PackageManager.PERMISSION_GRANTED) { Log.i("permissions", "phone number granted"); READ_PHONE_NUMBERS_GRANTED = true; } if (hasReadCallLogPermission == PackageManager.PERMISSION_GRANTED) { Log.i("permissions", "call log granted"); READ_CALL_LOG_GRANTED = true; } if ((!READ_PHONE_STATE_GRANTED) | (!READ_PHONE_NUMBERS_GRANTED) | (!READ_CALL_LOG_GRANTED)) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_NUMBERS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_CALL_LOG}, REQUEST_CODE_PARAMS); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case REQUEST_CODE_PARAMS: if (grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_GRANTED) { READ_PHONE_STATE_GRANTED = true; READ_PHONE_NUMBERS_GRANTED = true; READ_CALL_LOG_GRANTED = true; Log.i("permissions", "phone state & nums allowed"); } case REQUEST_CODE_READ_PHONE_NUMBERS: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { READ_PHONE_NUMBERS_GRANTED = true; Log.i("permissions", "phone numbers allowed"); } } } } Receiver code UpdatedCallReceiver.java:
package roninapps.callblocker; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.TelephonyManager; import android.util.Log; public class UpdatedCallReceiver extends BroadcastReceiver { private String incomingCallNumber; private String phoneState; @Override public void onReceive(Context context, Intent intent) { Log.i("UReceiver", "onReceive"); phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE); if (phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) { Log.i("UReceiver", "ringing"); incomingCallNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); Log.i("UReceiver", "n " + incomingCallNumber); } if (phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { Log.i("UReceiver", "offhook"); } if (phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)) { Log.i("UReceiver", "idle"); } } } Manifest code:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="roninapps.callblocker"> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/> <uses-permission android:name="android.permission.READ_CALL_LOG"/> <uses-feature android:name="android.hardware.telephony"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".UpdatedCallReceiver" android:enabled="true" android:exported="true"> <intent-filter android:priority="100"> <action android:name="android.intent.action.PHONE_STATE" android:enabled="true"/> </intent-filter> </receiver> </application> </manifest> In the logs, the receiver writes that it takes the intent twice, and for the first time, the caller does not catch the number (null). In the original version, without identifying the number, took it once. After adding the permission READ_CALL_LOG and determining the number, I began to catch it twice. Without permission, the number does not determine, gives an error (lack of permission). I tried to rewrite the receiver from scratch, without determining the number, but it still catches twice. Question: a) what could be the problem b) how to deal with it without refusing to identify the number (this is the whole point) give further.