2020.09.23

This commit is contained in:
2020-09-23 18:04:22 +08:00
parent c72030f8b2
commit bea9f1c45c
4 changed files with 133 additions and 1 deletions

View File

@@ -1,7 +1,11 @@
package com.tt.activity; package com.tt.activity;
import android.view.KeyEvent;
import android.widget.Toast;
import com.tt.base.BaseActivity; import com.tt.base.BaseActivity;
import com.tt.ttutils.R; import com.tt.ttutils.R;
import com.tt.ttutils.java.ToastUtil;
public class JMainActivity extends BaseActivity { public class JMainActivity extends BaseActivity {
@@ -19,4 +23,33 @@ public class JMainActivity extends BaseActivity {
protected void initData() { protected void initData() {
} }
// 用来计算返回键的点击间隔时间
private long exitTime = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_DOWN) {
if ((System.currentTimeMillis() - exitTime) > 1000) {
//弹出提示,可以有多种方式
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
private long mPreClickTime=0;
private void lazyExit() {
if (System.currentTimeMillis() - mPreClickTime > 1000) {
ToastUtil.show("再按一次,退出");
mPreClickTime = System.currentTimeMillis();
} else {
finish();
}
}
} }

View File

@@ -1,8 +1,18 @@
package com.tt.ttutils.java; package com.tt.ttutils.java;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build; import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.WindowManager;
import androidx.annotation.RequiresApi;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@@ -40,6 +50,85 @@ public class DevicesUtils {
} }
return serial; return serial;
} }
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static String getAndroiodScreenProperty(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getRealMetrics(dm);
int width = dm.widthPixels; // 屏幕宽度(像素)
int height = dm.heightPixels; // 屏幕高度(像素)
float density = dm.density; // 屏幕密度0.75 / 1.0 / 1.5
int densityDpi = dm.densityDpi; // 屏幕密度dpi120 / 160 / 240
// 屏幕宽度算法:屏幕宽度(像素)/屏幕密度
int screenWidth = (int) (width / density); // 屏幕宽度(dp)
int screenHeight = (int) (height / density);// 屏幕高度(dp)
Log.e("h_bl", "屏幕宽度(像素):" + width);
Log.e("h_bl", "屏幕高度(像素):" + height);
Log.e("h_bl", "屏幕密度0.75 / 1.0 / 1.5" + density);
Log.e("h_bl", "屏幕密度dpi120 / 160 / 240" + densityDpi);
Log.e("h_bl", "屏幕宽度dp" + screenWidth);
Log.e("h_bl", "屏幕高度dp" + screenHeight);
return screenWidth+"×"+screenHeight;
}
public static String getIMEI(Context context) {
String IMEI = "unknow";
String IMEI1, IMEI2, IMEI3;
//获取手机设备号
TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//8.0及以后版本获取
// if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
// IMEI1 = TelephonyMgr.getDeviceId();
// } else
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// try {
// Method method = TelephonyMgr.getClass().getMethod("getImei");
// IMEI2 = (String) method.invoke(TelephonyMgr);
// } catch (Exception e) {
// e.printStackTrace();
// Log.e("getIMEI", e.getMessage());
// }
// } else {//9.0到10.0获取
IMEI = Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
}
Log.e("IMEI:", "IMEI: " + IMEI);
return IMEI.toUpperCase();
}
public static String getProperty(String key, String defaultValue) {
String value = defaultValue;
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class, String.class);
value = (String) (get.invoke(c, key, "unknown"));
} catch (Exception e) {
e.printStackTrace();
} finally {
return value;
}
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static String getMachine(Context context) {
String device = Build.MODEL;//机型
String imei = getIMEI(context);
String system_version = Build.VERSION.RELEASE;
String firmware_version =getProperty("ro.build.display.id", "获取失败") ;
String rom = getProperty("ro.custom.build.version", "获取失败");
String screen_rate=getAndroiodScreenProperty(context);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("device",device);
jsonObject.put("imei",imei);
jsonObject.put("system_version",system_version);
jsonObject.put("firmware_version",firmware_version);
jsonObject.put("rom",rom);
jsonObject.put("screen_rate",screen_rate);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject.toString();
}
} }

View File

@@ -1,7 +1,17 @@
package com.tt.ttutils.kt package com.tt.ttutils.kt
import java.util.logging.Handler import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.widget.Toast
class ToastUtil { class ToastUtil {
private val mhandler: Handler = object : Handler(Looper.getMainLooper()) {}
fun init(context: Context) {
}
} }

Binary file not shown.