2.5 mac地址存储文件(mac获取失败就随机一个);监听返回按键,提示是否退出程序的提示窗口
This commit is contained in:
@ -2,7 +2,9 @@ package chaoran.business.activity;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.ActivityInfo;
|
||||
@ -493,5 +495,22 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
|
||||
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.SocketException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Random;
|
||||
|
||||
import chaoran.business.BuildConfig;
|
||||
|
||||
@ -67,12 +68,29 @@ public class LocalAddressUtil {
|
||||
@SuppressLint("JavascriptInterface")
|
||||
@JavascriptInterface
|
||||
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
|
||||
try {
|
||||
return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "02:00:00:00:00:02";
|
||||
return generateRandomMacAddress();
|
||||
}
|
||||
}
|
||||
String macAddress = null;
|
||||
@ -84,7 +102,7 @@ public class LocalAddressUtil {
|
||||
networkInterface = NetworkInterface.getByName("wlan0");
|
||||
}
|
||||
if (networkInterface == null) {
|
||||
return "02:00:00:00:00:02";
|
||||
return generateRandomMacAddress();
|
||||
}
|
||||
byte[] addr = networkInterface.getHardwareAddress();
|
||||
for (byte b : addr) {
|
||||
@ -94,12 +112,28 @@ public class LocalAddressUtil {
|
||||
buf.deleteCharAt(buf.length() - 1);
|
||||
}
|
||||
macAddress = buf.toString();
|
||||
} catch (SocketException e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "02:00:00:00:00:02";
|
||||
macAddress = generateRandomMacAddress();
|
||||
}
|
||||
if (macAddress.length() < 4) {
|
||||
macAddress = generateRandomMacAddress();
|
||||
}
|
||||
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")
|
||||
@JavascriptInterface
|
||||
public String getInfo(int type){
|
||||
|
||||
Reference in New Issue
Block a user