判断每天是否为首次登陆
//每天首次登陆
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String dateStr = sdf.format(new Date());//最新的时间
LogHelp.i("Device","date:"+dateStr);
String firstLogin = UserData.getFirstLogin();//获取SharedPreference备份的时间
if(firstLogin == null || "".equals(firstLogin) || !dateStr.equals(firstLogin)){
//如果是第一次安装或者卸载重装,或者不是同一天,就执行记录
UserData.setFirstLogin(dateStr);
LogHelp.i("Device","firstLogin:");
getDeviceInfo();
}
保存时间
public static void setFirstLogin(String firstLogin){
SPHelp.getInstance().put(Constant.SP.firstLogin,firstLogin);
}
public static String getFirstLogin(){
String firstLogin = SPHelp.getInstance().getString(Constant.SP.firstLogin);
return firstLogin;
}
记录设备信息
/*
信息记录
*/
public void getDeviceInfo(Data data) {
TelephonyManager mTm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
//登录设备标识
String deviceId = mTm.getDeviceId();
LogHelp.i("Device", "deviceId:" + deviceId);
//登录渠道标识
String channel = App.getAppChannel();
LogHelp.i("Device", "channel:" + channel);
//登录设备类型
String deviceType = "android";
//设备序列号
String serialNum = mTm.getSimSerialNumber();
LogHelp.i("Device", "serialNum:" + serialNum);
//手机机型
String deviceModel = android.os.Build.MODEL;
LogHelp.i("Device", "deviceModel:" + deviceModel);
//登陆ID
int userId = data.getUserId();
LogHelp.i("Device", "userId:" + userId);
//登陆凭证
String xuehuToken = data.getXuehuToken();
LogHelp.i("Device", "xuehuToken:" + xuehuToken);
//是否模拟器
boolean isEmulator = ("000000000000000".equals(deviceId) ? true : false);
LogHelp.i("Device", "isEmulator:" + isEmulator);
//Android sdk版本号
int sdkInt = Build.VERSION.SDK_INT;
LogHelp.i("Device", "sdkInt:" + sdkInt);
//设备操作系统和版本
String deviceOS = "android" + Build.VERSION.RELEASE;
LogHelp.i("Device", "deviceOS:" + deviceOS);
//联网方式
ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
int connectType = networkInfo.getType();
LogHelp.i("Device", "connectType:" + connectType);
String typeName = networkInfo.getTypeName();
LogHelp.i("Device", "connectType2:" + typeName);
//设备MAC地址
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String deviceMac = info.getMacAddress();
LogHelp.i("Device", "deviceMac:" + deviceMac);
//设备IP地址
int ipAddress = info.getIpAddress();
String deviceIp = "";
if (ipAddress > 0) {
deviceIp = intToIp(ipAddress);
} else {
deviceIp = getLocalIpAddress();
}
LogHelp.i("Device", "deviceIp:" + deviceIp);
//时间戳毫秒数
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
LogHelp.i("Device", "timestamp:" + timestamp);
//手机卡imsi号
String cardImsi = mTm.getSubscriberId();
LogHelp.i("Device", "cardImsi:" + cardImsi);
//是否root
boolean isRoot = false;
if ((new File("/system/bin/su").exists()) || (new File("/system/xbin/su").exists())){
isRoot = true;
}
LogHelp.i("Device", "isRoot:" + isRoot);
//androidId
String androidId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
LogHelp.i("Device", "androidId:" + androidId);
//客户端自生成uuid
String uuid = new UUID(androidId.hashCode(), ((long) deviceId.hashCode() << 32) | serialNum.hashCode()).toString();
LogHelp.i("Device", "uuid:" + uuid);
//客户端新UUID
String clientUUID = UUID.randomUUID().toString();
LogHelp.i("Device", "clientUUID:" + clientUUID);
//设备信息属性
String fingerPrint = "";
//是否支持蓝牙
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean bluetooth = (mBluetoothAdapter == null ? false : true);
LogHelp.i("Device", "bluetooth:" + bluetooth);
PackageManager pm = getPackageManager();
//是否有相机
boolean camera = false;
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) ||
pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
camera = true;
}
LogHelp.i("Device", "camera:" + camera);
//获取apk签名
int appSignInt = Integer.parseInt(SignUtils.getSingInfo(this));
LogHelp.i("Device", "appSignInt:" + appSignInt);
//Dex文件的校验码
String apkPath = LoginActivity.this.getPackageCodePath();
long crc = 0;
try {
ZipFile zipfile = new ZipFile(apkPath);
ZipEntry dexentry = zipfile.getEntry("classes.dex");
crc = dexentry.getCrc();
LogHelp.i("Device", "crc:" + crc);
} catch (IOException e) {
e.printStackTrace();
}
//是否支持多点触摸
boolean multiTouch = pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);
LogHelp.i("Device", "multiTouch:" + multiTouch);
//是否支持重力感应器
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor accSensor = sm.getDefaultSensor(Sensor.TYPE_GRAVITY);
boolean sensorAcc = (accSensor == null ? false : true);
LogHelp.i("Device", "sensorAcc:" + sensorAcc);
//是否支持光感器
Sensor lightSensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
boolean sensorLight = (lightSensor == null ? false : true);
LogHelp.i("Device", "sensorLight:" + sensorLight);
//是否支持磁场传感器
Sensor magneticSensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
boolean sensorMagnetic = (magneticSensor == null ? false : true);
LogHelp.i("Device", "sensorMagnetic:" + sensorMagnetic);
//是否支持方向传感器
Sensor oriSensor = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
boolean sensorOri = (oriSensor == null ? false : true);
LogHelp.i("Device", "sensorOri:" + sensorOri);
LoginAPI.getLoginInfo(deviceId, channel, deviceType, deviceModel, uuid,xuehuToken, userId,
deviceOS, connectType, deviceMac, deviceIp, timestamp, cardImsi, isRoot, androidId,
bluetooth, camera, appSignInt, clientUUID, crc, fingerPrint, isEmulator, multiTouch,
sdkInt, sensorAcc, sensorLight, sensorMagnetic, sensorOri, serialNum,
new BaseCallBack<BaseResponseEntity>() {
@Override
public void onSuccess(Response<BaseResponseEntity> response) {
}
@Override
public void onFailure(StateException exception) {
}
});
}
获取渠道工具类
public static String getAppChannel() {
try {
return getApplication().getPackageManager().getApplicationInfo(getApplication().getPackageName(), PackageManager.GET_META_DATA).metaData.getString("UMENG_CHANNEL");
} catch (PackageManager.NameNotFoundException e) {
if (null != e) {
LogHelp.e(getApplication().getPackageName(), e);
}
return "xuehu";
}
}
获取签名工具类
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import java.io.ByteArrayInputStream;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
/**
* apk 签名工具类
*/
public class SignUtils {
public static String getSingInfo(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
android.content.pm.Signature[] signs = packageInfo.signatures;
android.content.pm.Signature sign = signs[0];
String signNumber = parseSignature(sign.toByteArray());
return signNumber;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String parseSignature(byte[] signature) {
try {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(signature));
String pubKey = cert.getPublicKey().toString();
String signNumber = cert.getSerialNumber().toString();
return signNumber;
} catch (CertificateException e) {
e.printStackTrace();
}
return null;
}
}
获取IP地址工具类
private String intToIp(int ipAddress) {
return (ipAddress & 0xFF) + "." +
((ipAddress >> 8) & 0xFF) + "." +
((ipAddress >> 16) & 0xFF) + "." +
(ipAddress >> 24 & 0xFF);
}
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}