问题描述
我正在尝试创建一个呼叫号码的应用程序,一旦接收者接听电话,它就会执行某些操作(说),然后它应自动挂断。
所以,我所做的是,在broadcastreceiver的onReceive方法中,一旦调用开始,我等待一段时间后延迟usign处理程序,然后调用cutTheCall()方法挂断。
但是,呼叫在呼叫开始时就挂断了,而没有调用cutTheCall()方法
CallReceiver [broadCastReceiver的子类]
package com.example.multicall;
import android.content.broadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.widget.Toast;
import com.example.multicall.MainActivity;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static android.content.Context.TELEPHONY_SERVICE;
public class CallReceiver extends broadcastReceiver {
private Handler handler1 = new Handler();
private static int wait = 0 ;
private Runnable WaitToCutCall = new Runnable() {
@Override
public void run() {
if(wait < 1) {
wait = wait + 1;
handler1.postDelayed(this,20000);
}
else
{
wait =0 ;
}
}
};
@Override
public void onReceive(Context context,Intent intent)
{
if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK) )
{
showToast(context,"Call Started..");
MainActivity.changeAllowNewCall(false);
WaitToCutCall.run();
showToast( context,"Waiting Over");
cutTheCall( context );
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE))
{
showToast( context,"Call Ended..");
MainActivity.changeAllowNewCall(true);
}
}
void showToast( Context context,String message)
{
Toast toast = Toast.makeText(context,message,Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0);
toast.show();
}
private void cutTheCall(Context context)
{
showToast(context,"End Call");
TelephonyManager tm = (TelephonyManager)context.getSystemService(TELEPHONY_SERVICE);
Method m1 = null;
try {
m1 = tm.getClass().getDeclaredMethod("getITelephony");
} catch (NoSuchMethodException e) {
e.printstacktrace();
}
m1.setAccessible(true);
Object iTelephony = null;
try {
iTelephony = m1.invoke(tm);
} catch (illegalaccessexception e) {
e.printstacktrace();
} catch (InvocationTargetException e) {
e.printstacktrace();
}
Method m3 = null;
try {
m3 = iTelephony.getClass().getDeclaredMethod("endCall");
} catch (NoSuchMethodException e) {
e.printstacktrace();
}
try {
m3.invoke(iTelephony);
} catch (illegalaccessexception e) {
e.printstacktrace();
} catch (InvocationTargetException e) {
e.printstacktrace();
}
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver
android:name=".CallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)