我尝试了很多链接,但是我还不够好找到合适的代码。我设法使用此代码阻止了调用。
public class BlockCallReceiver extends BroadcastReceiver{
Context context;
@Override
public void onReceive(Context context, Intent intent) {
Bundle myBundle = intent.getExtras();
if (myBundle != null)
{
System.out.println("--------Not null-----");
try
{
if (intent.getAction().equals("android.intent.action.PHONE_STATE"))
{
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
System.out.println("--------in state-----");
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
// Incoming call
String incomingNumber =intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("--------------my number---------"+incomingNumber);
// this is main section of the code,. could also be use for particular number.
// Get the boring old TelephonyManager.
TelephonyManager telephonyManager =(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
// Get the getITelephony() method
Class<?> classTelephony = Class.forName(telephonyManager.getClass().getName());
Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
// Ignore that the method is supposed to be private
methodGetITelephony.setAccessible(true);
// Invoke getITelephony() to get the ITelephony interface
Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);
// Get the endCall method from ITelephony
Class<?> telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
// Invoke endCall()
methodEndCall.invoke(telephonyInterface);
}
}
}
catch (Exception ex)
{
// Many things can go wrong with reflection calls
ex.printStackTrace();
}
}
}
}
和我的清单
<receiver android:name=".utils.BlockCallReceiver" >
<intent-filter android:priority="100" >
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
有效。但即使是应用程序也不在前台/甚至在活动应用程序中都没有被阻止。
我试图在我的Launcher Activity中注册这个BlockCallReceiver类,因为我想在应用程序未打开时允许调用。
所以在onPause
我尝试了答案中给出的一些代码,但是由于我的知识不足,它没有用。
这是我试过的一个例子,但我被困了
ComponentName receiver = new ComponentName(MainActivity.this, BlockCallReceiver.class);
PackageManager pm = MainActivity.this.getPackageManager();
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
如果有人可以指导我在应用关闭时重新允许通话,我真的很感激。
编辑一个:
如果我在下面使用
public void onPause() {
super.onPause();
MainActivity.this.unregisterReceiver(mybroadcast);
}
它抛出了一个错误java.lang.IllegalArgumentException:
接收器未注册!是的我没有在我的活动中在清单上的那个东西上注册它。我知道有些事情是错的但却无法理解。
编辑两个:@Imen Nmn的回答很有帮助。我会尝试动态注册而不是Manifest。但是我怀疑在Manifest中没有注册B.receiver会有什么问题吗?
答案 0 :(得分:2)
无需删除您在Manifest中声明的内容。使用PackageManager
启用/禁用Manifest中声明的BroadcastReceiver
。
在主活动中调用此方法以检查状态
private void checkBroadCastRec() {
component = new ComponentName(getApplicationContext(), YourBoradcastRecClassName.class);
int status = getApplicationContext().getPackageManager().getComponentEnabledSetting(component);
if(status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
Log.d("Check stat","receiver is enabled");
} else if(status == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
Log.d("Check stat","receiver is disabled");
}
}
您可以使用以下代码段启用/禁用作为您的逻辑!
//Disable
getApplicationContext().getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
//Enable
getApplicationContext().getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED , PackageManager.DONT_KILL_APP);
答案 1 :(得分:1)
要停止broadcastReceiver,您应该致电: yourContext .unregisterReceiver( yourBroadcastReceiver );
如果你注意到这个例子,在 onResume 中有 this.registerReceiver(the_receiver,过滤器); ,并且在 onPause 中有< / p>
<强> this.unregisterReceiver(the_receiver); 强>
这是一个例子:
public class Android示例扩展了Activity {
private BroadcastReceiver the_receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context c, Intent i) {
}
};
// Set When broadcast event will fire.
private IntentFilter filter = new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onResume() {
// Register reciever if activity is in front
this.registerReceiver(the_receiver, filter);
super.onResume();
}
@Override
protected void onPause() {
// Unregister reciever if activity is not in front
this.unregisterReceiver(the_receiver);
super.onPause();
}
}