Commit 59811504 authored by jyx's avatar jyx

添加友盟工具类

parent b1abd5e6
package com.wenshu.youyou.ui.activitys;
import android.Manifest;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.text.TextUtils;
import java.lang.reflect.Method;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Locale;
/**
* @author jyx
* @date 2021/4/30
* @des
*/
public class UmengUtil {
public static String getDeviceInfo(Context context) {
try {
org.json.JSONObject json = new org.json.JSONObject();
android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String device_id = null;
if (checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
device_id = tm.getDeviceId();
}
String mac = getMac(context);
json.put("mac", mac);
if (TextUtils.isEmpty(device_id)) {
device_id = mac;
}
if (TextUtils.isEmpty(device_id)) {
device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),
android.provider.Settings.Secure.ANDROID_ID);
}
json.put("device_id", device_id);
return json.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getMac(Context context) {
String mac = "";
if (context == null) {
return mac;
}
if (Build.VERSION.SDK_INT < 23) {
mac = getMacBySystemInterface(context);
} else {
mac = getMacByJavaAPI();
if (TextUtils.isEmpty(mac)) {
mac = getMacBySystemInterface(context);
}
}
return mac;
}
@TargetApi(9)
private static String getMacByJavaAPI() {
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface netInterface = interfaces.nextElement();
if ("wlan0".equals(netInterface.getName()) || "eth0".equals(netInterface.getName())) {
byte[] addr = netInterface.getHardwareAddress();
if (addr == null || addr.length == 0) {
return null;
}
StringBuilder buf = new StringBuilder();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
return buf.toString().toLowerCase(Locale.getDefault());
}
}
} catch (Throwable e) {
}
return null;
}
private static String getMacBySystemInterface(Context context) {
if (context == null) {
return "";
}
try {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (checkPermission(context, Manifest.permission.ACCESS_WIFI_STATE)) {
WifiInfo info = wifi.getConnectionInfo();
return info.getMacAddress();
} else {
return "";
}
} catch (Throwable e) {
return "";
}
}
public static boolean checkPermission(Context context, String permission) {
boolean result = false;
if (context == null) {
return result;
}
if (Build.VERSION.SDK_INT >= 23) {
try {
Class clazz = Class.forName("android.content.Context");
Method method = clazz.getMethod("checkSelfPermission", String.class);
int rest = (Integer) method.invoke(context, permission);
if (rest == PackageManager.PERMISSION_GRANTED) {
result = true;
} else {
result = false;
}
} catch (Throwable e) {
result = false;
}
} else {
PackageManager pm = context.getPackageManager();
if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
result = true;
}
}
return result;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment