【发布时间】: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