我正在开发呼叫和短信阻止应用程序,我遇到了无法从phonestatelistener获取传入号码的问题。我的代码如下所示:
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_RINGING:
System.out.println("incoming number PhoneStateListener:" + incomingNumber);
if (sBlockCall>0){
MuteAudio(getContext());
//如果号码与我们的阻止号码匹配,则拒绝来电
boolean callShouldBeBlocked = true;
for(int i = 0; i<sWhiteList.size(); i++){
if(sIncomingNumber.contains(sWhiteList.get(i).mNumber)){
UnMuteAudio(getContext());
System.out.println("CONTACT IS IN WHITE LIST");
callShouldBeBlocked = false;
break;
}
}
if(callShouldBeBlocked){
System.out.println("BLOCK CALL");
disconnectPhoneItelephony();
}
} else {
UnMuteAudio(getContext());
}
//IT WORKS
break;
}
}
所以我的程序工作不像我期望的那样工作,因为它总是返回null作为传入的数字。
我的清单看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="oleksandr.ivanets.dontwakemeapp">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".PhoneStateReceiver"
android:enabled="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999999999">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
答案 0 :(得分:0)
尝试添加此权限,在我看来,这是可行的。
<uses-permission android:name="android.permission.READ_CALL_LOG"/>