package com.newland; import java.lang.reflect.Field; import java.lang.reflect.Method; import com.android.internal.telephony.ITelephony; import android.app.Service; import android.content.Context; import android.telecom.TelecomManager; import android.telephony.TelephonyManager; import android.util.Log; public class PhoneUtils { /** * 从TelephonyManager中实例化ITelephony,并返回 */ static public ITelephony getITelephony(TelephonyManager telMgr) throws Exception { Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod( "getITelephony"); getITelephonyMethod.setAccessible(true);// 私有化函数也能使用 return (ITelephony) getITelephonyMethod.invoke(telMgr); } public static int getSDKVersionNumber() { int sdkVersion; try { sdkVersion = Integer.valueOf(android.os.Build.VERSION.SDK_INT); } catch (NumberFormatException e) { sdkVersion = 0; } return sdkVersion; } //判断手机是否处于正在通话中 public static boolean phoneIsInUse(Context context) { int sdkVer = getSDKVersionNumber(); if(sdkVer >= 23) { return phoneIsInUse6_0(context); } else { return phoneIsInUse5_0(context); } } //Android如何判断手机是否处于正在通话中,Android 6.0之前(具体版本没追溯)用的方法(在Android 5.1上好用) public static boolean phoneIsInUse5_0(Context context) { boolean phoneInUse = false; TelephonyManager mTelephonyManager = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE); Class c = TelephonyManager.class; Method getITelephonyMethod = null; try { getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null); getITelephonyMethod.setAccessible(true); ITelephony iTelephony = (ITelephony) getITelephonyMethod.invoke( mTelephonyManager, (Object[]) null); phoneInUse = !iTelephony.isIdle(); } catch (Exception e) { e.printStackTrace(); } return phoneInUse; } //Android 6.0之后用以上方法不好用了,遍寻源码及网上资料得已下方法(在Android 6.0上好用): public static boolean phoneIsInUse6_0(Context context){ TelecomManager tm = (TelecomManager)context.getSystemService(Context.TELECOM_SERVICE); return tm.isInCall(); } static public void printAllInform(Class clsShow) { try { // 取得所有方法 Method[] hideMethod = clsShow.getDeclaredMethods(); int i = 0; for (; i < hideMethod.length; i++) { Log.e("method name", hideMethod[i].getName()); } // 取得所有常量 Field[] allFields = clsShow.getFields(); for (i = 0; i < allFields.length; i++) { Log.e("Field name", allFields[i].getName()); } } catch (SecurityException e) { // throw new RuntimeException(e.getMessage()); e.printStackTrace(); } catch (IllegalArgumentException e) { // throw new RuntimeException(e.getMessage()); e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }