技术标签: android Android工具类
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class GetIpAndDisplayUtil {
public static String getIp(){
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() && inetAddress instanceof Inet4Address) { //IPv4地址
return inetAddress.getHostAddress();
}
}
}
return "";
} catch (SocketException ex) {
return "";
}
}
/**
* 判断IP地址的合法性,这里采用了正则表达式的方法来判断 return true,合法
*/
public static boolean ipCheck(String text) {
if (text != null && !text.isEmpty()) {
// 定义正则表达式
String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
// 判断ip地址是否与正则表达式匹配
if (text.matches(regex)) {
// 返回判断信息
return true;
} else {
// 返回判断信息
return false;
}
}
return false;
}
//可能会不包含下方导航栏的高度
public static String getDisplay(Activity activity){
Display display = activity.getWindowManager().getDefaultDisplay();
Point point = new Point();
display.getSize(point);
int width = point.x;
int height = point.y;
return width+"*"+height;
}
public static String getRealMetrics(Activity activity){
DisplayMetrics metric = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metric);
int width = metric.widthPixels; // 宽度(PX)
int height = metric.heightPixels; // 高度(PX)
float density = metric.density; // 密度(0.75 / 1.0 / 1.5)
int densityDpi = metric.densityDpi;
return width+"*"+height;
}
public static String getScreenType(Activity activity){
DisplayMetrics metric = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metric);
int width = metric.widthPixels; // 宽度(PX)
int height = metric.heightPixels; // 高度(PX)
if(width>height){
return "横屏";
}else{
return "竖屏";
}
}
public static String getScreenType(Context context){
DisplayMetrics dm2 = context.getResources().getDisplayMetrics();
int width = dm2.widthPixels; // 宽度(PX)
int height = dm2.heightPixels; // 高度(PX)
if(width>height){
return "横屏";
}else{
return "竖屏";
}
}
public static String getOutSize(Activity activity){
Point outSize = new Point();
activity.getWindowManager().getDefaultDisplay().getRealSize(outSize);
int x = outSize.x;
int y = outSize.y;
return x+"*"+y;
}
public static String getOutMetrics (Activity activity){
DisplayMetrics outMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getRealMetrics(outMetrics);
int widthPixel = outMetrics.widthPixels;
int heightPixel = outMetrics.heightPixels;
return widthPixel+"*"+heightPixel;
}
public static int getDisplayWidth(Activity activity){
DisplayMetrics metric = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metric);
int width = metric.widthPixels; // 宽度(PX)
int height = metric.heightPixels; // 高度(PX)
return width;
}
public static int getDisplayHeight(Activity activity){
DisplayMetrics metric = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metric);
int width = metric.widthPixels; // 宽度(PX)
int height = metric.heightPixels; // 高度(PX)
return height;
}
public static int getDisplayWidth(Context context){
DisplayMetrics dm = new DisplayMetrics();
dm = context.getResources().getDisplayMetrics();
int width = dm.widthPixels;
int height = dm.heightPixels;
return width;
}
public static int getDisplayHeight(Context context){
DisplayMetrics dm = new DisplayMetrics();
dm = context.getResources().getDisplayMetrics();
int width = dm.widthPixels;
int height = dm.heightPixels;
return height;
}
}
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.NetworkInterface;
import java.net.SocketException;
public class GetMacUtil {
public static String getWlanMacAddress(Context context) {
WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
if (!wifiMgr.isWifiEnabled())
{
//必须先打开,才能获取到MAC地址
wifiMgr.setWifiEnabled( true );
//wifiMgr.setWifiEnabled( false );
}
wifiInfo.getMacAddress();
String macAddress = null;
StringBuffer buf = new StringBuffer();
NetworkInterface networkInterface = null;
try {
//networkInterface = NetworkInterface.getByName("eth1");
// if (networkInterface == null) {
// networkInterface = NetworkInterface.getByName("wlan0");
// }
networkInterface = NetworkInterface.getByName("wlan0");
if (networkInterface == null) {
return "02:00:00:00:00:00";
}
byte[] addr = networkInterface.getHardwareAddress();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
macAddress = buf.toString();
} catch (SocketException e) {
e.printStackTrace();
return "02:00:00:00:00:00";
}
Log.i("----getMacAddress--1--", "" + buf.toString());
return macAddress;
}
// Android 6.0以上获取WiFi的Mac地址
//由于android6.0对wifi mac地址获取进行了限制,用原来的方法获取会获取到02:00:00:00:00:00这个固定地址。
//但是可以通过读取节点进行获取"/sys/class/net/wlan0/address"
public static String getWlanMacAddress2() {
try {
return loadFileAsString("/sys/class/net/wlan0/address")
.toUpperCase().substring(0, 17);
} catch (IOException e) {
e.printStackTrace();
return "02:00:00:00:00:00";
}
}
public static String getWlanMacAddress3() {
String macSerial = "02:00:00:00:00:00";
String str = "";
try
{
Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; null != str;)
{
str = input.readLine();
if (str != null)
{
macSerial = str.trim();// 去空格
break;
}
}
} catch (IOException ex) {
// 赋予默认值
ex.printStackTrace();
}
return macSerial;
}
public static String getEthMacAddress(Context context) {
WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
if (!wifiMgr.isWifiEnabled())
{
//必须先打开,才能获取到MAC地址
wifiMgr.setWifiEnabled( true );
//wifiMgr.setWifiEnabled( false );
}
wifiInfo.getMacAddress();
String macAddress = null;
StringBuffer buf = new StringBuffer();
NetworkInterface networkInterface = null;
try {
//networkInterface = NetworkInterface.getByName("eth1");
// if (networkInterface == null) {
// networkInterface = NetworkInterface.getByName("wlan0");
// }
networkInterface = NetworkInterface.getByName("eth0");
if (networkInterface == null) {
return "02:00:00:00:00:00";
}
byte[] addr = networkInterface.getHardwareAddress();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
macAddress = buf.toString();
} catch (SocketException e) {
e.printStackTrace();
return "02:00:00:00:00:00";
}
Log.i("----getMacAddress--1--", "" + buf.toString());
return macAddress;
}
public static String getEthMacAddress2() {
try {
return loadFileAsString("/sys/class/net/eth0/address")
.toUpperCase().substring(0, 17);
} catch (IOException e) {
e.printStackTrace();
return "02:00:00:00:00:00";
}
}
private static String loadFileAsString(String filePath)
throws IOException {
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
}
reader.close();
return fileData.toString();
}
public static String getEthMacAddress3() {
String macSerial = "02:00:00:00:00:00";
String str = "";
try
{
Process pp = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address ");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; null != str;)
{
str = input.readLine();
if (str != null)
{
macSerial = str.trim();// 去空格
break;
}
}
} catch (IOException ex) {
// 赋予默认值
ex.printStackTrace();
}
return macSerial;
}
}
关于在python的tkinter界面中镶嵌mayplotlib动态图很多的时候,我们需要给客户展示一些比较美观的界面,中间就必然需要一些精美的图表,让客户看起来更加的专业,因此,我们就需要tkinter模块和maytplotlib模块相结合去制作这样的一个软件。其中最重要的就是如何让一个图表嵌入tkinter界面而不悬浮呢?而图嵌入后又怎么让它动起来?下面是我的一段代码,主要就是实现让图形嵌...
功能描述在数据库中创建一个新的表空间。语法格式CREATE TABLESPACE tablespace_name [ OWNER { new_owner | CURRENT_USER | SESSION_USER } ] LOCATION 'directory' [ WITH ( tablespace_option = value [, ... ] ) ]参数说明注意事项只有系统管理员可以创建表空间。不允许在一个事务块内部执行CREATE TABLESPACE。数据库集群中所有的CN和DN
使用Callable返回结果本文是Sun官方以Blog形式发布的Java核心技术窍门(JavaCoreTechTip)中的一个。本文主要介绍了Callable及其相关接口和类的使用,篇幅不长且易于理解,故翻译在了此处,相信对于准备或刚接触java.util.concurrent的朋友会有所帮助。(2008.05.31最后更新)自从Java平台的最开始,Runnable接口就已存在了。它允许你定义一...
上一篇文章介绍了数据的预处理部分,这一篇文章将会介绍神经网络模型的建立以及相关参数设计。 对于BP神经网络的模式识别来说,参数的设置对于神经网络的识别性能有着很大的影响。对于不同的问题来说应该有着其适当的参数设置。我的参数设置如下代码。%%%bp神经网络的参数设置NodeNum=12;%隐层节点数TypeNum=1;%输出节点数Epochs=500;%最大学习次数net=newff
axml<view style="width:714rpx;height:1190rpx;"> <view style="position:relative;width:714rpx;height:1190rpx;"> <!-- 主界面 --> <view a:if="{{mainBanner}}"> <view style="positon:relative;width:714rpx;height:1190rpx;ba
下载本文示例代码微软的MFC在Visual Studio 6.0中提供了一个新类CHtmlView,利用这个类,我们可以实现在基于文档视图结构的程序中显示HTML文件。 但是它是否可以用来在对话框中实现这一功能呢?我们不妨拿CHtmlView和CListView做一个比较,通过比较这两个类,我们会发现一些有趣的差别,MFC中CListView有一个对应的CListCtrl类用来在对话框中使用,而C...
GoWeb学习笔记学习的资料来自杨旭老师在B站的视频文章目录GoWeb学习笔记创建第一个Web程序(网页输出HelloWorld)HadleFunc源码使用HadleFunc,并创建内置函数的形式创建访问函数使用HadleFunc,并调用外部函数的形式创建访问函数创建访问监听和服务http.ListenAndServe源码http.Server源码(去除了源码中的注释)使用http.ListenAndServe创建监听和服务使用http.Server创建监听和服务整合实现直接创建通过外部函数和使用htt
为什么80%的码农都做不了架构师?>>> ...
在windows系统中,选取VSCode编辑器配置GO开发环境。
洛谷连接COGS题目连接 很好的一道题,我们要去处理区间问题,这里用到了一种很特殊,但是很好的算法。 因为天数是连续的,所以把第i天与第i+1天连接起来,费用为0、流量INF - a[i]。 然后再放一个第N+1天连接到T为INF的流,费用为0; 之后,我们要放对应的{ s[i], t[i], c[i] }是不是就可以往里面填充了,是不是可以直接s[i] -> t...
Problem DescriptionAekdyCoin is the most powerful boy in the group ACM_DIY, whose signature is valuable. In order to get his signature, you must first solve this problem. But don't be worried, becaus...
在主机上安装EPICS时,往往会遇到一些问题。初学者刚接手EPICS比较吃力,而且在网上的资源比较少,经验交流手册也写的比较简洁。我配置过一些电脑的EPICS环境,在这里总结一下EPICS安装过程中经常会遇到的一些问题和感悟,并提出一些解决方法,以供大家交流和讨论: 一、Linux平台下的EPICS: linux安装很简单,在官网下载合适版本的base包到&lt;ep...