2.5 mac地址存储文件(mac获取失败就随机一个);监听返回按键,提示是否退出程序的提示窗口
This commit is contained in:
@ -11,7 +11,7 @@ android {
|
|||||||
minSdk 28
|
minSdk 28
|
||||||
targetSdk 28
|
targetSdk 28
|
||||||
versionCode 1
|
versionCode 1
|
||||||
versionName "2.4"
|
versionName "2.5"
|
||||||
|
|
||||||
// 1.0 IDATA广播模式处理
|
// 1.0 IDATA广播模式处理
|
||||||
// 1.1 霍尼韦尔的监听修改(扫描网站二维码跳出程序,监听失效,调整)、斑马PDA广播模式设置
|
// 1.1 霍尼韦尔的监听修改(扫描网站二维码跳出程序,监听失效,调整)、斑马PDA广播模式设置
|
||||||
@ -37,6 +37,7 @@ android {
|
|||||||
// 2.2 安卓14以上,无法获取mac地址,修改成获取唯一id作为mac地址
|
// 2.2 安卓14以上,无法获取mac地址,修改成获取唯一id作为mac地址
|
||||||
// 2.3 适配IOT_Device:sc55g PDA 广播模式
|
// 2.3 适配IOT_Device:sc55g PDA 广播模式
|
||||||
// 2.4 适配 qualcomm:mc50 PDA 广播模式
|
// 2.4 适配 qualcomm:mc50 PDA 广播模式
|
||||||
|
// 2.5 mac地址存储文件(mac获取失败就随机一个);监听返回按键,提示是否退出程序的提示窗口
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
ndk {
|
ndk {
|
||||||
abiFilters 'armeabi-v7a'
|
abiFilters 'armeabi-v7a'
|
||||||
|
|||||||
@ -2,7 +2,9 @@ package chaoran.business.activity;
|
|||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.ActivityInfo;
|
import android.content.pm.ActivityInfo;
|
||||||
@ -493,5 +495,22 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
|
|||||||
super.onConfigurationChanged(newConfig);
|
super.onConfigurationChanged(newConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBackPressed() {
|
||||||
|
// 创建退出确认对话框
|
||||||
|
new AlertDialog.Builder(this)
|
||||||
|
.setTitle("提示")
|
||||||
|
.setMessage("是否退出程序?")
|
||||||
|
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
finish(); // 关闭当前 Activity
|
||||||
|
// 如果需要彻底退出应用(适用于多任务场景),可以添加:
|
||||||
|
// System.exit(0);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.setNegativeButton("取消", null)
|
||||||
|
.show();
|
||||||
|
// 不调用 super.onBackPressed(),避免直接退出
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -22,6 +22,7 @@ import java.net.InetAddress;
|
|||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
import chaoran.business.BuildConfig;
|
import chaoran.business.BuildConfig;
|
||||||
|
|
||||||
@ -67,12 +68,29 @@ public class LocalAddressUtil {
|
|||||||
@SuppressLint("JavascriptInterface")
|
@SuppressLint("JavascriptInterface")
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
public String getMacAddress(){//可以兼容安卓7以下
|
public String getMacAddress(){//可以兼容安卓7以下
|
||||||
|
SharedPreferences spf = context.getSharedPreferences("chaoran_mac", Context.MODE_PRIVATE);
|
||||||
|
String saveMac = spf.getString("chaoran_mac", "");
|
||||||
|
if (!"".equalsIgnoreCase(saveMac)) {
|
||||||
|
return saveMac;
|
||||||
|
}
|
||||||
|
SharedPreferences.Editor editor = spf.edit();
|
||||||
|
String androidMac = getAndroidMac();
|
||||||
|
editor.putString("chaoran_mac", androidMac);
|
||||||
|
editor.apply();
|
||||||
|
return androidMac;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取安卓的mac
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String getAndroidMac() {
|
||||||
if (Build.VERSION.SDK_INT >= 34) { // Android 14 is code-named Tiramisu
|
if (Build.VERSION.SDK_INT >= 34) { // Android 14 is code-named Tiramisu
|
||||||
try {
|
try {
|
||||||
return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
|
return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
|
||||||
}catch (Exception e) {
|
}catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return "02:00:00:00:00:02";
|
return generateRandomMacAddress();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String macAddress = null;
|
String macAddress = null;
|
||||||
@ -84,7 +102,7 @@ public class LocalAddressUtil {
|
|||||||
networkInterface = NetworkInterface.getByName("wlan0");
|
networkInterface = NetworkInterface.getByName("wlan0");
|
||||||
}
|
}
|
||||||
if (networkInterface == null) {
|
if (networkInterface == null) {
|
||||||
return "02:00:00:00:00:02";
|
return generateRandomMacAddress();
|
||||||
}
|
}
|
||||||
byte[] addr = networkInterface.getHardwareAddress();
|
byte[] addr = networkInterface.getHardwareAddress();
|
||||||
for (byte b : addr) {
|
for (byte b : addr) {
|
||||||
@ -94,12 +112,28 @@ public class LocalAddressUtil {
|
|||||||
buf.deleteCharAt(buf.length() - 1);
|
buf.deleteCharAt(buf.length() - 1);
|
||||||
}
|
}
|
||||||
macAddress = buf.toString();
|
macAddress = buf.toString();
|
||||||
} catch (SocketException e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return "02:00:00:00:00:02";
|
macAddress = generateRandomMacAddress();
|
||||||
|
}
|
||||||
|
if (macAddress.length() < 4) {
|
||||||
|
macAddress = generateRandomMacAddress();
|
||||||
}
|
}
|
||||||
return macAddress;
|
return macAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String generateRandomMacAddress() {
|
||||||
|
Random rand = new Random();
|
||||||
|
byte[] macBytes = new byte[6];
|
||||||
|
rand.nextBytes(macBytes);
|
||||||
|
macBytes[0] = (byte) (macBytes[0] & 0xfe | 0x02);
|
||||||
|
StringBuilder macAddress = new StringBuilder();
|
||||||
|
for (int i = 0; i < macBytes.length; i++) {
|
||||||
|
macAddress.append(String.format("%02X%s", macBytes[i], (i < macBytes.length - 1) ? ":" : ""));
|
||||||
|
}
|
||||||
|
return macAddress.toString();
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressLint("JavascriptInterface")
|
@SuppressLint("JavascriptInterface")
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
public String getInfo(int type){
|
public String getInfo(int type){
|
||||||
|
|||||||
Reference in New Issue
Block a user