Created
March 18, 2023 20:31
-
-
Save EvgenyPlaksin/ea197a939b4861fe1213390ec4d529cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lnight.getrestricteddata | |
import android.content.BroadcastReceiver | |
import android.content.Context | |
import android.content.Intent | |
import android.os.Build | |
import android.os.CancellationSignal | |
import android.provider.CallLog | |
import android.telephony.PhoneStateListener | |
import android.telephony.TelephonyCallback | |
import android.telephony.TelephonyManager | |
class ServiceReceiver : BroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent) { | |
println("") | |
val telephony = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | |
telephony.registerTelephonyCallback( | |
context.mainExecutor, | |
object : TelephonyCallback(), TelephonyCallback.CallStateListener { | |
override fun onCallStateChanged(state: Int) { | |
val cancellationSignal = CancellationSignal() | |
val cursor = context.contentResolver.query( | |
CallLog.Calls.CONTENT_URI, | |
null, | |
CallLog.Calls.TYPE + " = ?", | |
arrayOf(CallLog.Calls.INCOMING_TYPE.toString()), | |
CallLog.Calls.DATE + " DESC", | |
cancellationSignal | |
) | |
if (cursor != null && cursor.moveToFirst()) { | |
val indexPhone = cursor.getColumnIndex(CallLog.Calls.NUMBER) | |
val phoneNumber = | |
cursor.getString(if (indexPhone < 0) 0 else indexPhone) | |
println("incomingNumber : $phoneNumber") | |
cursor.close() | |
} | |
} | |
}) | |
} else { | |
telephony.listen(object : PhoneStateListener() { | |
@Deprecated("Deprecated in Java") | |
override fun onCallStateChanged(state: Int, incomingNumber: String) { | |
super.onCallStateChanged(state, incomingNumber) | |
println("incomingNumber : $incomingNumber") | |
} | |
}, PhoneStateListener.LISTEN_CALL_STATE) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Manifest: