Files
tianrunCRM/Assets/trCRM/Plugins/Android/java/com/newland/PhoneUtils.java
2020-08-26 19:56:45 +08:00

94 lines
2.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<TelephonyManager> 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();
}
}
}