Mat*_*imB 2 android telephonymanager
我需要自动接听电话(取决于来源电话号码).这时,我可以结束来电,但不能接受'.
我找到了几个例子,这就是我现在所拥有的.
添加到AndroidManifest:
<permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
...
<receiver android:name=".panic.IncomingCallReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
public class IncomingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
MyPhoneStateListener listener = new MyPhoneStateListener(context);
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
public class MyPhoneStateListener extends PhoneStateListener {
Context context;
public MyPhoneStateListener(Context context) {
super();
this.context = context;
}
@Override
public void onCallStateChanged(int state, String callingNumber) {
super.onCallStateChanged(state, callingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//handle out going call
acceptCallIfBlocked(callingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
//handle in coming call
acceptCallIfBlocked(callingNumber);
break;
default:
break;
}
}
private void acceptCallIfBlocked(String callingNumber) {
if (shouldAcceptCall()) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
// Java reflection to gain access to TelephonyManager's
// ITelephony getter
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
final Object invoke = m.invoke(tm);
c = Class.forName(invoke.getClass().getName());
m = c.getDeclaredMethod("endCall");
// m = c.getDeclaredMethod("answerRingingCall"); // NOT WORKING
m.setAccessible(true); // Make it accessible
final Object invoke1 = m.invoke(invoke);
} catch (Throwable ignored) {
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以用这段代码成功结束调用,但是我需要执行answerRingingCall才能完成我需要的东西.
我得到了这个例外:
用户10056和当前进程都没有android.permission.MODIFY_PHONE_STATE
我在Lollipop上运行但需要在尽可能多的设备中执行.有帮助吗?
拦截呼叫仅适用于2.2 - 4.0. 除了电话制造商证书之外,Google已阻止MODIFY_PHONE_STATE权限
Google将拦截电话限制为安全功能.简而言之,你不能故意这样做.
您可以在Android开发人员文档中看到它:
public static final String MODIFY_PHONE_STATE
Added in API level 1
Allows modification of the telephony state - power on, mmi, etc. Does not include placing calls.
Not for use by third-party applications.
Constant Value: "android.permission.MODIFY_PHONE_STATE"
Run Code Online (Sandbox Code Playgroud)
请注意短语不供第三方应用程序使用.
| 归档时间: |
|
| 查看次数: |
865 次 |
| 最近记录: |