【问题标题】:How to request for call logs permission for my android application如何为我的 android 应用程序请求通话记录权限
【发布时间】:2019-09-17 11:45:03
【问题描述】:

我正在创建一个应用程序,它为我接收或拨打的电话执行一组特定的操作。但即使我提供了所需的权限,该应用程序也无法检测到呼叫者 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>

我的广播接收者:

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

【问题讨论】:

  • 您在哪个设备上测试?因为根据 Android P 的 Android 标准权限和方法已更改。
  • 设备是galaxy s9,上面运行着android 9。
  • 我也尝试过使用 TelephonyManager.EXTRA_INCOMING_NUMBER 但它也只给出空值。

标签: android phone-call telephonymanager


【解决方案1】:

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

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

要在广播接收器中获取电话号码,您需要实现以下代码

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,也许你会得到解决方案。

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

【讨论】:

  • 我也使用了上面的代码,但它并不是在所有设备上都有效。
  • @SagarDhawan 用户 TelecomManager 而不是 TemephonyManager 用于最新的操作系统版本和旧的当前版本。
猜你喜欢
  • 2019-04-23
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 2015-10-24
相关资源
最近更新 更多