在我的应用程序中,我想阻止特定的传入号码。我会谷歌,并关注Blocking Incoming call - Android。但是这段代码对我不起作用。我正在测试 android 2.3.5
这里我没有活动课。 >>第一类是扩展BroadcastReceiver。
的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blocknumber"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
MainActivity.class
public class MainActivity extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String blockedNumbers[] = { "xxxxxxxxxx", "xxxxxxxxxx" };
Bundle b = intent.getExtras();
/*
* String incommingNumber = b
* .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
*/
String incommingNumber = b.getString("incoming_number");
Log.e("Incomming number========>", incommingNumber);
// String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
for (int i = 0; i < blockedNumbers.length; i++) {
Log.e("Incomming >>>>>>>>>>>>>========>", "" + i);
if (incommingNumber.equalsIgnoreCase(blockedNumbers[i])) {
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class<?> c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephonyService = (ITelephony) m
.invoke(telephony);
// telephonyService.silenceRinger();
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
ITelephony
public interface ITelephony {
boolean endCall();
void answerRingingCall();
//void silenceRinger();
}
答案 0 :(得分:1)
最后,解决问题, 创建IDL接口以获取核心Telephony服务 包名称必须为 com.android.internal.telephony
FileName:ITelephony.aidl
//首先,我正在创建ITelephony.java
对于创建 .aidl 文件New > File
并撰写ITelephony.aidl
然后按照Blocking a call without user intervention in android with an example.
步骤操作