Android中常用的一些方法总结的工具类
生活随笔
收集整理的這篇文章主要介紹了
Android中常用的一些方法总结的工具类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
多個工具類,包含IP地址獲取,資源圖片獲取,元分的轉換,銀行圖片的獲取,獲取UUID等
import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.text.DecimalFormat; import java.util.Enumeration; import java.util.UUID; import java.util.regex.Pattern;import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.provider.Settings; import android.telephony.TelephonyManager;public class YhshUtils {protected static final String PREFS_FILE = "device_id";protected static final String PREFS_DEVICE_ID = "device_id";private static final Pattern IPV4_PATTERN = Pattern.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");public static String getDeviceUniqueId(Context context) {SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0);String id = prefs.getString(PREFS_DEVICE_ID, null);String deviceUniqueId;//device unique idif (id != null) {deviceUniqueId = id;} else {String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);try {if (androidId != null && !"9774d56d682e549c".equals(androidId)) {deviceUniqueId = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")).toString();} else {String deviceId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();deviceUniqueId = (deviceId != null || "Unknown".equals(deviceId)) ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")).toString() : UUID.randomUUID().toString();}} catch (UnsupportedEncodingException e) {throw new RuntimeException(e);}prefs.edit().putString(PREFS_DEVICE_ID, deviceUniqueId).apply();}return deviceUniqueId;}@SuppressWarnings("MissingPermission")public static String getIMEIandMAC(Context context) {String imei_flag = "yhsh_mobile_android";try {TelephonyManager mTm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);//獲取IMEIString imei = mTm.getDeviceId();//獲取SIM卡的IMSIString imsi = mTm.getSubscriberId();//獲取MAC地址WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);WifiInfo wifiInfo = wifiManager.getConnectionInfo();String result = wifiInfo.getMacAddress();imei_flag = imei + "_" + result + "_" + imsi;} catch (Exception e) {e.printStackTrace();}return imei_flag;}/*** 獲得一個UUID** @return String UUID*/public static String getUUID() {String s = UUID.randomUUID().toString();//去掉“-”符號return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);}/*** 獲取資源文件--Drawable*/public static Drawable getResDrawable(Context context, String res_name) {InputStream is = null;try {is = context.getAssets().open(res_name);} catch (IOException e) {e.printStackTrace();}Bitmap bitmap = BitmapFactory.decodeStream(is);Drawable drawable = new BitmapDrawable(null, bitmap);return drawable;}/*** 分-->元 轉換-用于顯示*/public static String fen2Yuan(String total_fee) {DecimalFormat df = new DecimalFormat("#,##0.00 ");return df.format(new BigDecimal(Double.parseDouble(total_fee)).movePointLeft(2));}public static String yuan2Fen(String total_fee_Yuan) {return yuan2FenLong(total_fee_Yuan);}public static String yuan2FenLong(String total_fee_Yuan) {String yuan_raw = total_fee_Yuan.replace(",", "").trim();BigDecimal fenBd = new BigDecimal(yuan_raw).multiply(new BigDecimal(100));fenBd = fenBd.setScale(0, BigDecimal.ROUND_HALF_UP);return String.valueOf(fenBd.longValue());}/*** 獲取IP地址的方法** @return*/public static String getLocalIpAddress() {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() && isIPv4Address(inetAddress.getHostAddress())) {return inetAddress.getHostAddress();}}}} catch (SocketException ex) {ex.getLocalizedMessage();}return "192.0.0.1";}public static boolean isIPv4Address(String input) {return IPV4_PATTERN.matcher(input).matches();}/*** 獲取銀行圖標*/public static Drawable getResBankDrawable(Context context, String bank_name) {InputStream is = null;String res_name = "unknown.png";try {if (bank_name != null) {switch (bank_name) {case "?":res_name = "unknown.png";break;case "北京銀行":res_name = "bank_bj.png";break;case "光大銀行":res_name = "bank_gd.png";break;case "廣發銀行":res_name = "bank_gf.png";break;case "工商銀行":res_name = "bank_gs.png";break;case "華夏銀行":res_name = "bank_hx.png";break;case "建設銀行":res_name = "bank_js.png";break;case "交通銀行":res_name = "bank_jt.png";break;case "民生銀行":res_name = "bank_ms.png";break;case "農業銀行":res_name = "bank_ny.png";break;case "平安銀行":res_name = "bank_pa.png";break;case "浦發銀行":res_name = "bank_pf.png";break;case "上海銀行":res_name = "bank_sh.png";break;case "興業銀行":res_name = "bank_xy.png";break;case "郵政儲蓄":res_name = "bank_yz.png";break;case "中國銀行":res_name = "bank_zg.png";break;case "招商銀行":res_name = "bank_zs.png";break;case "中信銀行":res_name = "bank_zx.png";break;}}is = context.getAssets().open(res_name);} catch (IOException e) {e.printStackTrace();}Bitmap bitmap = BitmapFactory.decodeStream(is);Drawable drawable = new BitmapDrawable(bitmap);return drawable;}}?
總結
以上是生活随笔為你收集整理的Android中常用的一些方法总结的工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果xrmax是双卡吗
- 下一篇: Android中网络请求创建单个线程池的