响铃后,我需要结束通话(有点像未接来电).我使用broadcastReceiver和PhonestateListener监视状态.
问题是我正在接收广播以及电话号码,并且侦听器工作良好-它进入交换机内部并执行预期的大小写,但未执行ITelephony接口的endCall方法.这是我的代码:
表现:
<uses-permission androID:name="androID.permission.CALL_PHONE" /><uses-permission androID:name="androID.permission.READ_PHONE_STATE" /><uses-permission androID:name="androID.permission.MODIFY_PHONE_STATE" tools:ignore="ProtectedPermissions" /><uses-permission androID:name="androID.permission.PROCESS_OUTGOING_CALLS"/><uses-permission androID:name="androID.permission.READ_CALL_LOG" /><uses-permission androID:name="androID.permission.WRITE_CALL_LOG" /><receiver androID:name=".EndCall" androID:enabled="true" > <intent-filter> <action androID:name="androID.intent.action.NEW_OUTGOING_CALL" /> </intent-filter></receiver>
EndCall.java
public class EndCall extends broadcastReceiver { @OverrIDe public voID onReceive(Context context, Intent intent) { Log.i("DeBUG", "Received a broadcast..."); String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); Log.i("DeBUG", "Phone number: " + phonenumber); TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); EndCallListener callListener = new EndCallListener(context, tm); tm.Listen(callListener, PhonestateListener.ListEN_CALL_STATE); }}
EndCallListener.java:
public class EndCallListener extends PhonestateListener { Context context; TelephonyManager tm; public EndCallListener(Context context, TelephonyManager tm) { this.context = context; this.tm = tm; } @OverrIDe public voID onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_OFFHOOK: Log.i("DeBUG", "OFFHOOK"); try { Log.i("DeBUG", "InsIDe try..."); Class c = Class.forname(tm.getClass().getname()); Method m = c.getDeclaredMethod("getITelephony"); ITelephony telephonyService = (ITelephony) m.invoke(tm); //nothing happens after this... if(telephonyService.endCall()) { Log.i("DeBUG", "Call ended..."); } else { Log.i("DeBUG", "Call not ended..."); } } catch (ClassNotFoundException e) { e.printstacktrace(); } catch (NoSuchMethodException e) { e.printstacktrace(); } catch (InvocationTargetException e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); } catch (remoteexception e) { e.printstacktrace(); } break; case TelephonyManager.CALL_STATE_IDLE: Log.i("DeBUG", "IDLE"); break; } }}
ITelephony.aIDl
package com.androID.internal.telephony; interface ITelephony { boolean endCall(); voID answerRingingCall(); voID silenceRinger();}
解决方法:
最后一条评论使事情变得不清楚(在问题中).“它确实结束了通话,但是问题是我想在响铃或通话后的某个时间结束通话.”
因此,通话已结束,但您希望在响铃时或一段时间后结束?
这里有2种情况:
-响铃时:您需要检查CALL_STATE_RINGING,然后转到How to reject a call programatically android并检查那里的技术
>进行调用之后的一段时间:只需在稍后阶段从Runnable(或其他机制)中调用endCall()方法,您只需在处理程序上postDelayed()并设置某种延迟时间即可.
在Runnable中运行的代码中,我将检查调用是否仍在进行中
我可能不合时宜,建议您澄清您的评论/问题
总结以上是内存溢出为你收集整理的java-摘机时结束拨出电话全部内容,希望文章能够帮你解决java-摘机时结束拨出电话所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)