我想通过我的应用以编程方式接收来电。
我尝试了一些代码,但它不起作用。
下面是我结束通话的代码。
TelephonyManager tm = (TelephonyManager) ctx
.getSystemService(Context.TELEPHONY_SERVICE);
try {
if (tm == null) {
// this will be easier for debugging later on
throw new NullPointerException("tm == null");
}
tm.getClass().getMethod("endCall").invoke(tm);//answerRingingCall
} catch (Exception e) {
Log.e("sdsd", "Unable to use the Telephony Manager directly.", e);
}
}
使用此代码我可以结束任何来电,但是当我将 "endCall" 更改为 "answerRingingCall" 时。它没有收到来 self 的应用程序的调用,你能帮助解决这个问题吗?
关于权限,我无法将此权限应用于应用。
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
请参阅附件屏幕截图。
它显示权限仅授予系统应用程序。如何解决这个问题。
提前致谢
最佳答案
这可能有帮助
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// called when someone is ringing to this phone
Toast.makeText(ctx,
"Incoming: "+incomingNumber,
Toast.LENGTH_LONG).show();
break;
}
}
}
tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
关于android - 如何以编程方式接收来电?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30615911/