使用的是耳机模拟,网上的代码可以接通,但是会出现异常。
新建类PhoneManager
public class PhoneManager {
////放开耳机按钮
private static void mediaPhone1(Context paramContext)
{
try
{
Intent localIntent = new Intent("android.intent.action.MEDIA_BUTTON");
localIntent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0L, System.currentTimeMillis(), 0, 79, 0, 0, 0, 0, 0));
paramContext.sendOrderedBroadcast(localIntent, "android.permission.CALL_PRIVILEGED");
return;
}
catch (Exception localException)
{
localException.printStackTrace();
}
}
////放开耳机按钮
private static void mediaPhone2(Context paramContext)
{
try
{
Intent localIntent = new Intent("android.intent.action.MEDIA_BUTTON");
localIntent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0L, System.currentTimeMillis(), 1, 79, 0, 0, 0, 0, 0));
paramContext.sendOrderedBroadcast(localIntent, "android.permission.CALL_PRIVILEGED");
return;
}
catch (Exception localException)
{
localException.printStackTrace();
}
}
////插耳机 前
private static void mediaPhone3(Context paramContext)
{
try
{
Intent localIntent = new Intent("android.intent.action.HEADSET_PLUG");
localIntent.putExtra("state", 1);
localIntent.putExtra("name", "Headset");
localIntent.putExtra("microphone", "1");
paramContext.sendOrderedBroadcast(localIntent, "android.permission.CALL_PRIVILEGED");
return;
}
catch (Exception localException)
{
localException.printStackTrace();
}
}
//插耳机 后
private static void mediaPhone4(Context paramContext)
{
try
{
Intent localIntent = new Intent("android.intent.action.HEADSET_PLUG");
localIntent.putExtra("status", 0);
localIntent.putExtra("app", "cc");
localIntent.putExtra("TAG", "CcService");
localIntent.putExtra("state", 0);
localIntent.putExtra("name", "Headset");
localIntent.putExtra("microphone", "1");
paramContext.sendOrderedBroadcast(localIntent, "android.permission.CALL_PRIVILEGED");
return;
}
catch (Exception localException)
{
localException.printStackTrace();
}
}
public static void answerCall(Context paramContext)
throws Exception
{
mediaPhone1(paramContext);
mediaPhone2(paramContext);
mediaPhone3(paramContext);
mediaPhone4(paramContext);
}
}
在接听按钮的点击事件中直接调用
try {
PhoneManager.answerCall(getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
这样即可实现接听电话。
缺陷,实际有耳机插入的时候该怎么办?
接通之前判断是否有耳机已经插入,然后再分情况处理
用一下方法也可以实现
try {
Method method = Class.forName("android.os.ServiceManager")
.getMethod("getService", String.class);
IBinder binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE});
ITelephony telephony = ITelephony.Stub.asInterface(binder);
telephony.answerRingingCall();
} catch (NoSuchMethodException e) {
Log.d("Sandy", "", e);
} catch (ClassNotFoundException e) {
Log.d("Sandy", "", e);
}catch (Exception e) {
Log.d("Sandy", "", e);
try{
Log.e("Sandy", "for version 4.1 or larger");
Intent intent = new Intent("android.intent.action.MEDIA_BUTTON");
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
intent.putExtra("android.intent.extra.KEY_EVENT",keyEvent);
sendOrderedBroadcast(intent,"android.permission.CALL_PRIVILEGED");
} catch (Exception e2) {
Log.d("Sandy", "", e2);
Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT,keyEvent);
sendOrderedBroadcast(meidaButtonIntent, null);
}