[英]Blocking incoming call in android
I want to block/unblock incoming call in my android app. 我想在我的Android应用中阻止/取消阻止来电。 I am able to do that using below code. 我可以使用下面的代码来做到这一点。
telephony = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
customPhoneListener = new PhoneCallStateListener(MainActivity.this, vSMS);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
telephony.listen(customPhoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
Toast.makeText(MainActivity.this, "START", Toast.LENGTH_SHORT).show();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
telephony.listen(customPhoneListener,
PhoneStateListener.LISTEN_NONE);
Toast.makeText(MainActivity.this, "STOP", Toast.LENGTH_SHORT).show();
}
});
}
But problem is that when I click on start button call blocking service activated and then we go to home page call blocking is still in activate mode.(that's fine). 但是问题是,当我单击“开始”按钮时,呼叫阻止服务已激活,然后我们进入主页呼叫阻止仍处于激活模式。(很好)。 but when I open my app and press stop button then call doesn't unblock. 但是当我打开我的应用程序并按“停止”按钮时,通话不会解除阻止。 it still blocked. 它仍然被阻止。 why? 为什么? what I am doing wrong? 我做错了什么?
You are getting the telephone object using the activity context and the customPhoneListener
I guess is defined as instance variable in your activity. 您正在使用活动上下文获取电话对象, customPhoneListener
我认为customPhoneListener
被定义为活动中的实例变量。
But when you close the activity (by going to home screen) and open it again, you have a new context so the stop button almost does nothing. 但是,当您关闭活动(通过转到主屏幕)并再次将其打开时,您将拥有一个新的上下文,因此“停止”按钮几乎什么也不做。
To resolve this you can try the following: 要解决此问题,您可以尝试以下操作:
1- User application context 1-用户应用程序上下文
telephony = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
2- Ensure that the listener object customPhoneListener
is the same during the life cycle of your application not your activity. 2-确保在应用程序而非活动期间,侦听器对象customPhoneListener
相同。 Try to make it static
or define it in the application class
尝试使其变为static
或在application class
对其进行定义
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.