java - 如何以编程方式挂断奥利奥及以上的电话?
问题描述
我正在构建一个应用程序,它为用户提供了一种在激活服务后阻止所有来电的方法。
在针对较新的 SDK 版本(在我的情况下为 Oreo 及更高版本)方面,显然缺乏解决该问题的方法。我最接近解决方案的是this thread,但该问题没有公认的答案,在我的情况下也没有任何提供的答案。
以下是我也访问过的一些链接:
- 如何挂断Android中的拨出电话?
- 我可以在android中以编程方式挂断电话吗?
- 以编程方式结束android中的调用
- 如何制作可以阻止电话呼叫的原生 Android 应用程序
- 在 Android 上检测来电和去电
我的方法是使用前台服务并监听电话状态的变化。
@Override
public void onCreate() {
super.onCreate();
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CallStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
Log.d(TAG, "Called onCreate() method");
}
这是 CallStateListener
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String phoneNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
Log.d(TAG, "Phone is ringing");
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
try {
//Gain access to ITelephony getter
Class c = Class.forName(telephonyManager.getClass().getName());
Method method = c.getDeclaredMethod("getITelephony");
method.setAccessible(true);
com.android.internal.telephony.ITelephony telephony = (ITelephony) method.invoke(telephonyManager);
//Hung up the call
telephony.endCall();
} catch (Exception e) {
Log.d(TAG, "Exception occurred: " + e.getMessage());
e.printStackTrace();
}
}
}
}
我可以确认Phone is ringing
收到日志消息
我得到的例外是Exception occurred: MODIFY_PHONE_STATE permission required.
MODIFY_PHONE_STATE
第三方应用程序开发人员无法获得权限。我已经看到了修改 linter 的建议,但我仍然无法请求此权限,因此异常仍然存在。
在这些 SDK 版本中是否有其他挂断电话的方法?如果是这样,我很想被任命为正确的方向。谢谢你。
解决方案
推荐阅读
- javascript - 在 Javascriot 中使用原型继承的不可变属性
- kubernetes - 在管理可扩展性方面,在 Kubernetes 集群上部署带有 Debezium 的 Kafka 有什么区别?
- laravel - Firebase 在 Laravel 中使用 HTML 返回 404 错误
- javascript - 仅显示一次模态显示
- mysql - 按子查询表排序
- laravel - 如何检索一对多关系。此集合实例上不存在属性 [produks]
- linux - 我有一个按列排序的文本文件。如何在单个列中搜索匹配的字符串,然后打印整行
- c++ - 为什么要实例化此模板类特化?
- javascript - 如何添加 WebRTC ICE Candidate?
- javascript - p5向量减法'sub'返回错误