Android2.3以前的系統能夠經過反射機制調用ITelephone的方法來掛斷電話,由於Android2.3之後增長了對 permission android.permission.MODIFY_PHONE_STATE 的限制,以前的反射的方法不能用了,咱們能夠經過發送廣播的方式來接聽電話。java
示例代碼android
package com.example.android_3gtest; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.net.Uri; import android.os.Build; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.view.KeyEvent; public class PhoneUtil { public static String TAG = PhoneUtil.class.getSimpleName(); /** * 掛斷電話 * @param context */ public static void endCall(Context context) { try { Object telephonyObject = getTelephonyObject(context); if (null != telephonyObject) { Class telephonyClass = telephonyObject.getClass(); Method endCallMethod = telephonyClass.getMethod("endCall"); endCallMethod.setAccessible(true); endCallMethod.invoke(telephonyObject); } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } private static Object getTelephonyObject(Context context) { Object telephonyObject = null; try { // 初始化iTelephony TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); // Will be used to invoke hidden methods with reflection // Get the current object implementing ITelephony interface Class telManager = telephonyManager.getClass(); Method getITelephony = telManager.getDeclaredMethod("getITelephony"); getITelephony.setAccessible(true); telephonyObject = getITelephony.invoke(telephonyManager); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return telephonyObject; } /** * 經過反射調用的方法,接聽電話,該方法只在android 2.3以前的系統上有效。 * @param context */ private static void answerRingingCallWithReflect(Context context) { try { Object telephonyObject = getTelephonyObject(context); if (null != telephonyObject) { Class telephonyClass = telephonyObject.getClass(); Method endCallMethod = telephonyClass.getMethod("answerRingingCall"); endCallMethod.setAccessible(true); endCallMethod.invoke(telephonyObject); // ITelephony iTelephony = (ITelephony) telephonyObject; // iTelephony.answerRingingCall(); } } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } /** * 僞造一個有線耳機插入,並按接聽鍵的廣播,讓系統開始接聽電話。 * @param context */ private static void answerRingingCallWithBroadcast(Context context){ AudioManager localAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); //判斷是否插上了耳機 boolean isWiredHeadsetOn = localAudioManager.isWiredHeadsetOn(); if (!isWiredHeadsetOn) { Intent headsetPluggedIntent = new Intent(Intent.ACTION_HEADSET_PLUG); headsetPluggedIntent.putExtra("state", 1); headsetPluggedIntent.putExtra("microphone", 0); headsetPluggedIntent.putExtra("name", ""); context.sendBroadcast(headsetPluggedIntent); 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); context.sendOrderedBroadcast(meidaButtonIntent, null); Intent headsetUnpluggedIntent = new Intent(Intent.ACTION_HEADSET_PLUG); headsetUnpluggedIntent.putExtra("state", 0); headsetUnpluggedIntent.putExtra("microphone", 0); headsetUnpluggedIntent.putExtra("name", ""); context.sendBroadcast(headsetUnpluggedIntent); } else { 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); context.sendOrderedBroadcast(meidaButtonIntent, null); } } /** * 接聽電話 * @param context */ public static void answerRingingCall(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { //2.3或2.3以上系統 answerRingingCallWithBroadcast(context); } else { answerRingingCallWithReflect(context); } } /** * 打電話 * @param context * @param phoneNumber */ public static void callPhone(Context context, String phoneNumber) { if(!TextUtils.isEmpty(phoneNumber)){ try { Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phoneNumber)); context.startActivity(callIntent); } catch (Exception e) { e.printStackTrace(); } } } /** * 撥電話 * @param context * @param phoneNumber */ public static void dialPhone(Context context, String phoneNumber){ if(!TextUtils.isEmpty(phoneNumber)){ try { Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+ phoneNumber)); context.startActivity(callIntent); } catch (Exception e) { e.printStackTrace(); } } } }