接听 phone 电话
Answer phone call
我需要自动接听 phone 来电(取决于来源 phone 号码)。这个时候可以挂断来电,但是不能接听
我找到了几个例子,这就是我目前所拥有的。
添加到 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>
这是我的代码:
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) {
}
}
}
}
}
我可以使用此代码成功结束通话,但我需要执行 answerRingingCall 才能完成我需要的操作。
我遇到了这个异常:
Neither user 10056 nor current process has
android.permission.MODIFY_PHONE_STATE
我 运行 在 Lollipop 上,但需要在尽可能多的设备上执行。
有帮助吗?
拦截调用仅适用于 2.2 - 4.0。 Google has blocked the MODIFY_PHONE_STATE permission for anyone except for phone manufacturer certificates
Google 已将拦截呼叫限制为一项安全功能。简而言之,您不能故意这样做。
您可以在 Android developer documentation 中看到它:
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"
请注意短语不供第三方应用程序使用。
我需要自动接听 phone 来电(取决于来源 phone 号码)。这个时候可以挂断来电,但是不能接听
我找到了几个例子,这就是我目前所拥有的。
添加到 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>
这是我的代码:
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) {
}
}
}
}
}
我可以使用此代码成功结束通话,但我需要执行 answerRingingCall 才能完成我需要的操作。
我遇到了这个异常:
Neither user 10056 nor current process has android.permission.MODIFY_PHONE_STATE
我 运行 在 Lollipop 上,但需要在尽可能多的设备上执行。 有帮助吗?
拦截调用仅适用于 2.2 - 4.0。 Google has blocked the MODIFY_PHONE_STATE permission for anyone except for phone manufacturer certificates
Google 已将拦截呼叫限制为一项安全功能。简而言之,您不能故意这样做。
您可以在 Android developer documentation 中看到它:
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"
请注意短语不供第三方应用程序使用。