Reputation: 25
I am writing an Android app (kotlin) which logs incoming and outgoing calls (information about phone number and date). So far, I can obtain phone number from incoming calls, but failed to obtain it from outgoing ones.
I looked at tutorials and all of them suggest that I check the intent action and then get the number which I am trying to call.
in the manifest file I have added permissions and receiver
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<receiver android:name=".PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
in PhoneStateReceiver I'm trying to obtain phone numbers
class PhoneStateReceiver : BroadcastReceiver() {
private val TAG = "PhoneStateReceiver"
private var lastState = TelephonyManager.CALL_STATE_IDLE
override fun onReceive(context: Context?, intent: Intent?) {
checkCall(intent)
}
private fun checkCall(intent: Intent?) {
val state = intent?.getStringExtra(TelephonyManager.EXTRA_STATE)
if (intent?.action.equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
var number = intent?.getStringExtra(Intent.EXTRA_PHONE_NUMBER)
Log.d(TAG, "Outgoing number : $number")
}else {
if (lastState == TelephonyManager.CALL_STATE_IDLE) {
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
val number = intent?.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)
Log.d(TAG, "Incoming number : $number")
}
}
}
Log.d(TAG, state)
lastState = intState(state)
}
and in MainActivity I'm only asking for permissions:
class MainActivity : AppCompatActivity() {
private var PERMISSION_ALL = 1
private var PERMISSIONS = arrayOf(
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.PROCESS_OUTGOING_CALLS
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(!hasPermissions(this, Manifest.permission.READ_PHONE_STATE, Manifest.permission.PROCESS_OUTGOING_CALLS)){
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
}
private fun hasPermissions(context: Context, vararg permissions: String): Boolean = permissions.all {
ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}
}
The action when making an outgoing call should be Intent.ACTION_NEW_OUTGOING_CALL However, when I make an outgoing call this action appears to be Intent.PHONE_STATE
Why is that and how can I fix it?
Upvotes: 0
Views: 234
Reputation: 2103
Add both actions into one intent-filter
<receiver android:name=".PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Upvotes: 0