问题描述
在我的应用程序,我想阻止特定的传入number.I做谷歌,并遵循的。但是,这code是不工作的我,我正在测试的安卓2.3.5
在这里,我没有任何活动类。 >>第一类是扩展的BroadcastReceiver。
的manifest.xml
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.blocknumber
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =17/>
<使用-权限的Android:名称=android.permission.READ_PHONE_STATE/>
<使用-权限的Android:名称=android.permission.CALL_PHONE/>
<使用-权限的Android:名称=android.permission.MODIFY_PHONE_STATE/>
<使用-权限的Android:名称=android.permission.PROCESS_INCOMING_CALLS/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<接收机器人:名称=。MainActivity>
<意向滤光器>
<作用机器人:名称=android.intent.action.PHONE_STATE/>
&所述; /意图滤光器>
< /接收器>
< /用途>
MainActivity.class
公共类MainActivity扩展的BroadcastReceiver {
公共无效的onReceive(上下文的背景下,意图意图){
串blockedNumbers [] = {XXXXXXXXXX,XXXXXXXXXX};
叠B = intent.getExtras();
/ *
*字符串incommingNumber = B
* .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
* /
串incommingNumber = b.getString(incoming_number);
Log.e(进来的数量========>中,incommingNumber);
//字符串状态= intent.getStringExtra(TelephonyManager.EXTRA_STATE);
的for(int i = 0; I< blockedNumbers.length;我++){
Log.e(进来的>>>>>>>>>>>>> ======>中,+ I);
如果(incommingNumber.equalsIgnoreCase(blockedNumbers [I])){
TelephonyManager电话=(TelephonyManager)上下文
.getSystemService(Context.TELEPHONY_SERVICE);
尝试 {
类<> C =的Class.forName(telephony.getClass()的getName());
方法米= c.getDeclaredMethod(getITelephony);
m.setAccessible(真正的);
ITelephony telephonyService =(ITelephony)M
.invoke(电话);
// telephonyService.silenceRinger();
telephonyService.endCall();
}赶上(例外五){
e.printStackTrace();
}
}
}
}
}
ITelephony
公共接口ITelephony {
布尔结束呼叫();
无效answerRingingCall();
//无效silenceRinger();
}
最后,解决问题,创建IDL接口获取核心电话服务包的名称必须是 com.android.internal.telephony
文件名: ITelephony.aidl
//首先在这里我createing ITelephony.java
对于创建的 .aidl 文件最新及GT;文件
写 ITelephony.aidl
和遵循的步骤<一href="http://androiddesk.word$p$pss.com/2012/08/02/blocking-a-call-without-user-intervention-in-android/#comments"相对=nofollow>禁止在Android的呼叫,而无需用户干预的例子。
In my application I want to block the specific incoming number.I do google,and following Blocking Incoming call - Android. But this code is not working for me.I am testing on android 2.3.5
Here I have no activity class. >> First class is extends 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();
}
Finally, solve the issue,Create IDL interface for getting core Telephony servicepackage name must be com.android.internal.telephony
FileName : ITelephony.aidl
// First Here I am createing ITelephony.java
For the create .aidl file New > File
and write ITelephony.aidl
And follow the steps Blocking a call without user intervention in android with an example.
这篇关于如何拒绝/关闭特定的来电号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!