在Android开发过程中,有时候需要获取IP地址、mac地址、移动设备IP等,这里记录下方法,方便查找,也希望可以帮到需要的朋友。
1. 获取本地国家码
public static String getLocalCountry()
{
String code = Locale.getDefault().getCountry();
Log.d("country", code);
return code;
}
2. 判定是否是中国
private static boolean isZhRCN()
{
if (Locale.getDefault().getLanguage().equalsIgnoreCase("zh")
&& Locale.getDefault().getCountry().equalsIgnoreCase("CN") ) {
return true;
}
return false;
}
3. 获取手机系统语言
public static String getSystemLanguage()
{
String lan = Locale.getDefault().toString();
Log.w("syslan", lan);
if(isZhRCN())
return "zh-Hans";
else if (Locale.getDefault().getLanguage().equalsIgnoreCase("zh") && Locale.getDefault().getCountry().equalsIgnoreCase("hk"))
return "zh-HK";
else if(Locale.getDefault().getLanguage().equalsIgnoreCase("zh"))
return "zh-Hant";
return lan;
}
4. 获取移动设备本地IP
private static InetAddress getLocalInetAddress()
{
InetAddress ip = null;
try {
// 列举
Enumeration en_netInterface = NetworkInterface
.getNetworkInterfaces();
while (en_netInterface.hasMoreElements()) {// 是否还有元素
NetworkInterface ni = (NetworkInterface) en_netInterface
.nextElement();// 得到下一个元素
Enumeration en_ip = ni.getInetAddresses();// 得到一个ip地址的列举
while (en_ip.hasMoreElements())
{
ip = en_ip.nextElement();
if (!ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1)
break;
else
ip = null;
}
if (ip != null) {
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ip;
}
5. 获取本地Mac地址
public static String getLocalMac(String ipAddress) throws SocketException,
UnknownHostException
{
String strMacAddr = null;
try {
//获得IpD地址
InetAddress ip = getLocalInetAddress();
byte[] b = NetworkInterface.getByInetAddress(ip).getHardwareAddress();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; i++) {
if (i != 0) {
buffer.append(':');
}
String str = Integer.toHexString(b[i] & 0xFF);
buffer.append(str.length() == 1 ? 0 + str : str);
}
strMacAddr = buffer.toString().toUpperCase();
} catch (Exception e) {
e.printStackTrace();
}
return strMacAddr;
}
6. 获取设备Mac地址
public static String getDeviceMac() {
//获得IpD地址
InetAddress ip = getLocalInetAddress();
String ipAddress = ip.getHostAddress();
String macAddress = null;
try {
macAddress = getLocalMac(ipAddress);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return macAddress;
}
7. 获取设备token
public static byte[] getDeviceToken()
{
byte[] devicetoken = null;
// make sure you have READ_PHONE_STATE permission.
{
final String androidId = Secure.getString(activityInst.getContentResolver(), ANDROID_ID);
if (androidId != null && androidId.length() > 0)
{
if (!androidId.equals("9774d56d682e549c")
&& !androidId.equals("754d1a720b957ce6")
&& !androidId.equals("15742e48457c04d4")
&& !androidId.equals("b68276f4353a908f"))
devicetoken = androidId.getBytes();
}
}
if (devicetoken == null)
{
final String deviceid = ((TelephonyManager) activityInst.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId();
if (deviceid != null && deviceid.length() > 0)
{
devicetoken = deviceid.getBytes();
}
}
if (devicetoken == null)
devicetoken = Installation.id(activityInst);
return devicetoken;
}
8. 获取版本号
public static String getVersionString()
{
try {
PackageInfo pi = activityInst.getPackageManager().getPackageInfo(activityInst.getPackageName(), 0);
return pi.versionName;
} catch (NameNotFoundException e) {
Log.e(TAG, "failed to get version number");
}
return null;
}
9. 获取包名及设备型号
public static String getAndroidPackageName()
{
return activityInst.getPackageName();
}
public static String getDeviceModel()
{
return android.os.Build.MODEL;
}
public static String GetDeviceType()
{
return android.os.Build.MODEL + " " + android.os.Build.VERSION.RELEASE;
}
10. 打开链接
public static void openLink(String url)
{
if(url == null) return;
if (url.startsWith("http://") || url.startsWith("https://")) {
Intent fIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activityInst.startActivity(fIntent);
}
}
11. 唤醒屏幕
@SuppressLint("InvalidWakeLockTag")
private static void setBoolean(String systemSettingKey, boolean enabled) {
PowerManager pManager = ((PowerManager) activityInst.getSystemService(POWER_SERVICE));
if(enabled)
{
mWakeLock = pManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE, TAG);
mWakeLock.acquire();
}
else
{
if (mWakeLock != null) {
Log.v(TAG, "Releasing wakelock");
try {
mWakeLock.release();
mWakeLock = null;
} catch (Throwable th) {
// ignoring this exception, probably wakeLock was already released
}
}
}
Settings.System.putInt(mContentResolver,
systemSettingKey, enabled ? 1 : 0);
}