如何为我的 android 应用请求通话记录权限

How to request for call logs permission for my android application

我正在创建一个应用程序,它对我收到的电话执行一组特定的操作,或者make.But即使我提供了所需的权限,该应用程序也无法检测到呼叫者 ID。

我的清单:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" /> 

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

我的 BrodcastReciever:

  TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
                telephonyManager.listen(new PhoneStateListener(){
                    @Override
                    public void onCallStateChanged(int state, String incomingNumber) {
                        super.onCallStateChanged(state, incomingNumber);
                        System.out.println("incomingNumber : "+incomingNumber);
                        if(state == TelephonyManager.CALL_STATE_IDLE && clientContacts.containsKey(incomingNumber)) {
                            sharedPreferences.edit()
                                    .putString("callerName",clientContacts.get(incomingNumber))
                                    .apply();

                            final Intent intent1 = new Intent(context, ClientInteractionDialogService.class);
                            intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                            new Handler().postDelayed(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    ContextCompat.startForegroundService(context, intent1);
                                }
                            },500);
                        }
                    }
                },PhoneStateListener.LISTEN_CALL_STATE);

下面的代码第一次会给你空值,但之后,它总是会给你正确的数字

从用户那里获取 运行 时间权限。

要在广播接收器中获取 phone 号码,您需要执行以下代码

IncomingCallReceiver.kt

class IncomingCallReceiver : BroadcastReceiver() {


    override fun onReceive(context: Context?, intent: Intent?) {

        try {
            val newPhoneState = intent!!.getStringExtra(TelephonyManager.EXTRA_STATE)
            val bundle = intent.extras

            if (newPhoneState != null && newPhoneState == TelephonyManager.EXTRA_STATE_RINGING) {
                val phoneNumber = bundle!!.getString(TelephonyManager.EXTRA_INCOMING_NUMBER)

                if (phoneNumber != null) {
                    Toast.makeText(context, "Ring $phoneNumber", Toast.LENGTH_SHORT).show()
                }
             }
       } catch (ee: Exception) {
            Log.i("Telephony receiver", ee.message!!)
        }
    }

}

在清单文件中注册接收器

<receiver
            android:name="com.nip.callblock.IncomingCallReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />

            </intent-filter>
        </receiver>

这将在 onReceive 方法中为您提供来电号码

仔细阅读this也许你会得到解决方案。

如果您发现任何问题,请与我联系。