羊皮書APP(Android版)開發系列(十)Android開發常用工具類
阿新 • • 發佈:2019-02-13
日期格式化工具
package cn.studyou.baselibrary.utils;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 基本功能:日期格式化工具
* 建立:王傑
* 建立時間:13/3/14
* 郵箱:[email protected]
*/
public class DateFormatUtil {
private static SimpleDateFormat second = new SimpleDateFormat(
"yy-MM-dd hh:mm:ss");
private static SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat detailDay = new SimpleDateFormat("yyyy年MM月dd日");
private static SimpleDateFormat fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss" );
private static SimpleDateFormat tempTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat excelDate = new SimpleDateFormat("yyyy/MM/dd");
/**
* 格式化excel中的時間
* @param date
* @return
*/
public static String formatDateForExcelDate(Date date) {
return excelDate.format(date);
}
/**
* 將日期格式化作為檔名
* @param date
* @return
*/
public static String formatDateForFileName(Date date) {
return fileName.format(date);
}
/**
* 格式化日期(精確到秒)
*
* @param date
* @return
*/
public static String formatDateSecond(Date date) {
return second.format(date);
}
/**
* 格式化日期(精確到秒)
*
* @param date
* @return
*/
public static String tempDateSecond(Date date) {
return tempTime.format(date);
}
public static Date tempDateSecond(String str) {
try {
return tempTime.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return new Date();
}
/**
* 格式化日期(精確到天)
*
* @param date
* @return
*/
public static String formatDateDay(Date date) {
return day.format(date);
}
/**
* 格式化日期(精確到天)
*
* @param date
* @return
*/
public static String formatDateDetailDay(Date date) {
return detailDay.format(date);
}
/**
* 將double型別的數字保留兩位小數(四捨五入)
*
* @param number
* @return
*/
public static String formatNumber(double number) {
DecimalFormat df = new DecimalFormat();
df.applyPattern("#0.00");
return df.format(number);
}
/**
* 將字串轉換成日期
*
* @param date
* @return
* @throws Exception
*/
public static Date formateDate(String date) throws Exception {
return day.parse(date);
}
/**
* 將字元日期轉換成Date
* @param date
* @return
* @throws Exception
*/
public static Date parseStringToDate(String date) throws Exception {
return day.parse(date);
}
public static String formatDoubleNumber(double number) {
DecimalFormat df = new DecimalFormat("#");
return df.format(number);
}
}
轉換圖片顏色工具
package cn.studyou.baselibrary.utils;
import android.app.Activity;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
/**
* 基本功能:轉換圖片顏色工具
* 建立:王傑
* 建立時間:15/1/23
* 郵箱:[email protected]
*/
public class ConvertImageColor {
Activity activity;
public ConvertImageColor(Activity activity) {
this.activity = activity;
}
// 改變圖示的顏色
public Drawable changeImageColor(int drawable,int color){
Drawable myIcon = activity.getResources().getDrawable(drawable);
myIcon.setColorFilter(activity.getResources().getColor(color), PorterDuff.Mode.SRC_ATOP);
return myIcon;
}
}
app啟動引導頁控制工具
package cn.studyou.baselibrary.utils;
import android.content.Context;
import android.text.TextUtils;
/**
* 基本功能:app啟動引導頁控制
* 建立:王傑
* 建立時間:16/3/7
* 郵箱:[email protected]
*/
public class AppIntroUtil {
public static final int LMODE_NEW_INSTALL = 1; //再次啟動
public static final int LMODE_UPDATE = 2;//更新後第一次啟動
public static final int LMODE_AGAIN = 3;//首次安裝啟動
public static final String SHARENAME= "lastVersion";
private boolean isOpenMarked = false;
private int launchMode = LMODE_AGAIN; //啟動-模式
private static AppIntroUtil instance;
public static AppIntroUtil getThis() {
if (instance == null)
instance = new AppIntroUtil();
return instance;
}
/**
* 標記-開啟app,用於產生-是否首次開啟
* @param context
*/
public void markOpenApp(Context context) {
// 防止-重複呼叫
if (isOpenMarked)
return;
isOpenMarked = true;
String lastVersion = OperatingSharedPreferences.getString(context,SHARENAME,SHARENAME);
String thisVersion = VersionUtil.getVersion(context);
// 首次啟動
if (TextUtils.isEmpty(lastVersion)) {
launchMode = LMODE_NEW_INSTALL;
OperatingSharedPreferences.setString(context,SHARENAME,SHARENAME,thisVersion);
}
// 更新
else if (VersionUtil.compareVersion(lastVersion,thisVersion)) {
launchMode = LMODE_UPDATE;
OperatingSharedPreferences.setString(context, SHARENAME, SHARENAME, thisVersion);
}
// 二次啟動(版本未變)
else
launchMode = LMODE_AGAIN;
}
public int getLaunchMode() {
return launchMode;
}
/**
* 首次開啟,新安裝、覆蓋(版本號不同)
* @return
*/
public boolean isFirstOpen() {
return launchMode != LMODE_AGAIN;
}
}
Intent 工具
package cn.studyou.baselibrary.utils;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
/**
* 基本功能:Intent工具
* 建立:王傑
* 建立時間:16/3/7
* 郵箱:[email protected]
*/
public class IntentUtil {
private static final String TAG = IntentUtil.class.getSimpleName();
public static Intent getLauncherIntent() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_HOME);
return intent;
}
public static void logIntent(String tag, Intent intent) {
if (intent == null) {
return;
}
StringBuffer sb = new StringBuffer();
sb.append("\nAction:" + intent.getAction());
sb.append("\nData:" + intent.getData());
sb.append("\nDataStr:" + intent.getDataString());
sb.append("\nScheme:" + intent.getScheme());
sb.append("\nType:" + intent.getType());
Bundle extras = intent.getExtras();
if (extras != null && !extras.isEmpty()) {
for (String key : extras.keySet()) {
Object value = extras.get(key);
sb.append("\nEXTRA: {" + key + "::" + value + "}");
}
} else {
sb.append("\nNO EXTRAS");
}
Log.i(tag, sb.toString());
}
public static int sdkVersion() {
return new Integer(Build.VERSION.SDK).intValue();
}
public static void startDialer(Context context, String phoneNumber) {
try {
Intent dial = new Intent();
dial.setAction(Intent.ACTION_DIAL);
dial.setData(Uri.parse("tel:" + phoneNumber));
context.startActivity(dial);
} catch (Exception ex) {
Log.e(TAG, "Error starting phone dialer intent.", ex);
Toast.makeText(context,
"Sorry, we couldn't find any app to place a phone call!",
Toast.LENGTH_SHORT).show();
}
}
public static void startSmsIntent(Context context, String phoneNumber) {
try {
Uri uri = Uri.parse("sms:" + phoneNumber);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.putExtra("address", phoneNumber);
intent.setType("vnd.android-dir/mms-sms");
context.startActivity(intent);
} catch (Exception ex) {
Log.e(TAG, "Error starting sms intent.", ex);
Toast.makeText(context,
"Sorry, we couldn't find any app to send an SMS!",
Toast.LENGTH_SHORT).show();
}
}
public static void startEmailIntent(Context context, String emailAddress) {
try {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL,
new String[]{emailAddress});
context.startActivity(intent);
} catch (Exception ex) {
Log.e(TAG, "Error starting email intent.", ex);
Toast.makeText(context,
"Sorry, we couldn't find any app for sending emails!",
Toast.LENGTH_SHORT).show();
}
}
public static void startWebIntent(Context context, String url) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
context.startActivity(intent);
} catch (Exception ex) {
Log.e(TAG, "Error starting url intent.", ex);
Toast.makeText(context,
"Sorry, we couldn't find any app for viewing this url!",
Toast.LENGTH_SHORT).show();
}
}
}
手機資訊採集工具
package cn.studyou.baselibrary.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.SystemClock;
import android.telephony.TelephonyManager;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 基本功能:手機資訊採集工具
* 建立:王傑
* 建立時間:14/1/1
* 郵箱:[email protected]
*/
public class MobileUtil {
/**
* Print telephone info.
*/
public static String printMobileInfo(Context context) {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = dateFormat.format(date);
StringBuilder sb = new StringBuilder();
sb.append("系統時間:").append(time).append("\n");
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String IMSI = tm.getSubscriberId();
//IMSI前面三位460是國家號碼,其次的兩位是運營商代號,00、02是中國移動,01是聯通,03是電信。
String providerName = null;
if (IMSI != null) {
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
providerName = "中國移動";
} else if (IMSI.startsWith("46001")) {
providerName = "中國聯通";
} else if (IMSI.startsWith("46003")) {
providerName = "中國電信";
}
}
sb.append(providerName).append("\n").append(getNativePhoneNumber(context)).append("\n網路模式:").append(getNetType(context)).append("\nIMSI是:").append(IMSI);
sb.append("\nDeviceID(IMEI) :").append(tm.getDeviceId());
sb.append("\nDeviceSoftwareVersion:").append(tm.getDeviceSoftwareVersion());
sb.append("\ngetLine1Number :").append(tm.getLine1Number());
sb.append("\nNetworkCountryIso :").append(tm.getNetworkCountryIso());
sb.append("\nNetworkOperator :").append(tm.getNetworkOperator());
sb.append("\nNetworkOperatorName :").append(tm.getNetworkOperatorName());
sb.append("\nNetworkType :").append(tm.getNetworkType());
sb.append("\nPhoneType :").append(tm.getPhoneType());
sb.append("\nSimCountryIso :").append(tm.getSimCountryIso());
sb.append("\nSimOperator :").append(tm.getSimOperator());
sb.append("\nSimOperatorName :").append(tm.getSimOperatorName());
sb.append("\nSimSerialNumber :").append(tm.getSimSerialNumber());
sb.append("\ngetSimState :").append(tm.getSimState());
sb.append("\nSubscriberId :").append(tm.getSubscriberId());
sb.append("\nVoiceMailNumber :").append(tm.getVoiceMailNumber());
return sb.toString();
}
/**
* 列印系統資訊
* @return
*/
public static String printSystemInfo() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = dateFormat.format(date);
StringBuilder sb = new StringBuilder();
sb.append("_______ 系統資訊 ").append(time).append(" ______________");
sb.append("\nID :").append(Build.ID);
sb.append("\nBRAND :").append(Build.BRAND);
sb.append("\nMODEL :").append(Build.MODEL);
sb.append("\nRELEASE :").append(Build.VERSION.RELEASE);
sb.append("\nSDK :").append(Build.VERSION.SDK);
sb.append("\n_______ OTHER _______");
sb.append("\nBOARD :").append(Build.BOARD);
sb.append("\nPRODUCT :").append(Build.PRODUCT);
sb.append("\nDEVICE :").append(Build.DEVICE);
sb.append("\nFINGERPRINT :").append(Build.FINGERPRINT);
sb.append("\nHOST :").append(Build.HOST);
sb.append("\nTAGS :").append(Build.TAGS);
sb.append("\nTYPE :").append(Build.TYPE);
sb.append("\nTIME :").append(Build.TIME);
sb.append("\nINCREMENTAL :").append(Build.VERSION.INCREMENTAL);
sb.append("\n_______ CUPCAKE-3 _______");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
sb.append("\nDISPLAY :").append(Build.DISPLAY);
}
sb.append("\n_______ DONUT-4 _______");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
sb.append("\nSDK_INT :").append(Build.VERSION.SDK_INT);
sb.append("\nMANUFACTURER :").append(Build.MANUFACTURER);
sb.append("\nBOOTLOADER :").append(Build.BOOTLOADER);
sb.append("\nCPU_ABI :").append(Build.CPU_ABI);
sb.append("\nCPU_ABI2 :").append(Build.CPU_ABI2);
sb.append("\nHARDWARE :").append(Build.HARDWARE);
sb.append("\nUNKNOWN :").append(Build.UNKNOWN);
sb.append("\nCODENAME :").append(Build.VERSION.CODENAME);
}
sb.append("\n_______ GINGERBREAD-9 _______");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
sb.append("\nSERIAL :").append(Build.SERIAL);
}
return sb.toString();
}
/****
* 獲取網路型別
*
* @param context
* @return
*/
public static String getNetType(Context context) {
try {
ConnectivityManager connectMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectMgr.getActiveNetworkInfo();
if (info == null) {
return "";
}
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
return "WIFI";
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA) {
return "CDMA";
} else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE) {
return "EDGE";
} else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_0) {
return "EVDO0";
} else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_A) {
return "EVDOA";
} else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS) {
return "GPRS";
}
/*
* else if(info.getSubtype() ==
* TelephonyManager.NETWORK_TYPE_HSDPA){ return "HSDPA"; }else
* if(info.getSubtype() == TelephonyManager.NETWORK_TYPE_HSPA){
* return "HSPA"; }else if(info.getSubtype() ==
* TelephonyManager.NETWORK_TYPE_HSUPA){ return "HSUPA"; }
*/
else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_UMTS) {
return "UMTS";
} else {
return "3G";
}
} else {
return "";
}
} catch (Exception e) {
return "";
}
}
/**
* 獲取當前設定的電話號碼
*/
public static String getNativePhoneNumber(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String NativePhoneNumber = null;
NativePhoneNumber = telephonyManager.getLine1Number();
return String.format("手機號: %s", NativePhoneNumber);
}
/**
* IMSI是國際移動使用者識別碼的簡稱(International Mobile Subscriber Identity)
* IMSI共有15位,其結構如下:
* MCC+MNC+MIN
* MCC:Mobile Country Code,移動國家碼,共3位,中國為460;
* MNC:Mobile NetworkCode,行動網路碼,共2位
* 在中國,移動的程式碼為電00和02,聯通的程式碼為01,電信的程式碼為03
* 合起來就是(也是Android手機中APN配置檔案中的程式碼):
* 中國移動:46000 46002
* 中國聯通:46001
* 中國電信:46003
* 舉例,一個典型的IMSI號碼為460030912121001
*/
public static String getIMSI(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String IMSI = telephonyManager.getSubscriberId();
return IMSI;
}
/**
* IMEI是International Mobile Equipment Identity (國際移動裝置標識)的簡稱
* IMEI由15位數字組成的”電子串號”,它與每臺手機一一對應,而且該碼是全世界唯一的
* 其組成為:
* 1. 前6位數(TAC)是”型號核准號碼”,一般代表機型
* 2. 接著的2位數(FAC)是”最後裝配號”,一般代表產地
* 3. 之後的6位數(SNR)是”串號”,一般代表生產順序號
* 4. 最後1位數(SP)通常是”0″,為檢驗碼,目前暫備用
*/
public static String getIMEI(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String IMEI = telephonyManager.getDeviceId();
return IMEI;
}
/////_________________ 雙卡雙待系統IMEI和IMSI方案(see more on http://benson37.iteye.com/blog/1923946)
/**
* 雙卡雙待神機IMSI、IMSI、PhoneType資訊
* <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
*/
public static class TeleInfo {
public String imsi_1;
public String imsi_2;
public String imei_1;
public String imei_2;
public int phoneType_1;
public int phoneType_2;
@Override
public String toString() {
return "TeleInfo{" +
"imsi_1='" + imsi_1 + '\'' +
", imsi_2='" + imsi_2 + '\'' +
", imei_1='" + imei_1 + '\'' +
", imei_2='" + imei_2 + '\'' +
", phoneType_1=" + phoneType_1 +
", phoneType_2=" + phoneType_2 +
'}';
}
}
/**
* MTK Phone.
*
* 獲取 MTK 神機的雙卡 IMSI、IMSI 資訊
*/
public static TeleInfo getMtkTeleInfo(Context context) {
TeleInfo teleInfo = new TeleInfo();
try {
Class<?> phone = Class.forName("com.android.internal.telephony.Phone");
Field fields1 = phone.getField("GEMINI_SIM_1");
fields1.setAccessible(true);
int simId_1 = (Integer) fields1.get(null);
Field fields2 = phone.getField("GEMINI_SIM_2");
fields2.setAccessible(true);
int simId_2 = (Integer) fields2.get(null);
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Method getSubscriberIdGemini = TelephonyManager.class.getDeclaredMethod("getSubscriberIdGemini", int.class);
String imsi_1 = (String) getSubscriberIdGemini.invoke(tm, simId_1);
String imsi_2 = (String) getSubscriberIdGemini.invoke(tm, simId_2);
teleInfo.imsi_1 = imsi_1;
teleInfo.imsi_2 = imsi_2;
Method getDeviceIdGemini = TelephonyManager.class.getDeclaredMethod("getDeviceIdGemini", int.class);
String imei_1 = (String) getDeviceIdGemini.invoke(tm, simId_1);
String imei_2 = (String) getDeviceIdGemini.invoke(tm, simId_2);
teleInfo.imei_1 = imei_1;
teleInfo.imei_2 = imei_2;
Method getPhoneTypeGemini = TelephonyManager.class.getDeclaredMethod("getPhoneTypeGemini", int.class);
int phoneType_1 = (Integer) getPhoneTypeGemini.invoke(tm, simId_1);
int phoneType_2 = (Integer) getPhoneTypeGemini.invoke(tm, simId_2);
teleInfo.phoneType_1 = phoneType_1;
teleInfo.phoneType_2 = phoneType_2;
} catch (Exception e) {
e.printStackTrace();
}
return teleInfo;
}
/**
* MTK Phone.
*
* 獲取 MTK 神機的雙卡 IMSI、IMSI 資訊
*/
public static TeleInfo getMtkTeleInfo2(Context context) {
TeleInfo teleInfo = new TeleInfo();
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class<?> phone = Class.forName("com.android.internal.telephony.Phone");
Field fields1 = phone.getField("GEMINI_SIM_1");
fields1.setAccessible(true);
int simId_1 = (Integer) fields1.get(null);
Field fields2 = phone.getField("GEMINI_SIM_2");
fields2.setAccessible(true);
int simId_2 = (Integer) fields2.get(null);
Method getDefault = TelephonyManager.class.getMethod("getDefault", int.class);
TelephonyManager tm1 = (TelephonyManager) getDefault.invoke(tm, simId_1);
TelephonyManager tm2 = (TelephonyManager) getDefault.invoke(tm, simId_2);
String imsi_1 = tm1.getSubscriberId();
String imsi_2 = tm2.getSubscriberId();
teleInfo.imsi_1 = imsi_1;
teleInfo.imsi_2 = imsi_2;
String imei_1 = tm1.getDeviceId();
String imei_2 = tm2.getDeviceId();
teleInfo.imei_1 = imei_1;
teleInfo.imei_2 = imei_2;
int phoneType_1 = tm1.getPhoneType();
int phoneType_2 = tm2.getPhoneType();
teleInfo.phoneType_1 = phoneType_1;
teleInfo.phoneType_2 = phoneType_2;
} catch (Exception e) {
e.printStackTrace();
}
return teleInfo;
}
/**
* Qualcomm Phone.
* 獲取 高通 神機的雙卡 IMSI、IMSI 資訊
*/
public static TeleInfo getQualcommTeleInfo(Context context) {
TeleInfo teleInfo = new TeleInfo();
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class<?> simTMclass = Class.forName("android.telephony.MSimTelephonyManager");
Object sim = context.getSystemService("phone_msim");
int simId_1 = 0;
int simId_2 = 1;
Method getSubscriberId = simTMclass.getMethod("getSubscriberId", int.class);
String imsi_1 = (String) getSubscriberId.invoke(sim, simId_1);
String imsi_2 = (String) getSubscriberId.invoke(sim, simId_2);
teleInfo.imsi_1 = imsi_1;
teleInfo.imsi_2 = imsi_2;
Method getDeviceId = simTMclass.getMethod("getDeviceId", int.class);
String imei_1 = (String) getDeviceId.invoke(sim, simId_1);
String imei_2 = (String) getDeviceId.invoke(sim, simId_2);
teleInfo.imei_1 = imei_1;
teleInfo.imei_2 = imei_2;
Method getDataState = simTMclass.getMethod("getDataState");
int phoneType_1 = tm.getDataState();
int phoneType_2 = (Integer) getDataState.invoke(sim);
teleInfo.phoneType_1 = phoneType_1;
teleInfo.phoneType_2 = phoneType_2;
} catch (Exception e) {
e.printStackTrace();
}
return teleInfo;
}
/**
* Spreadtrum Phone.
*
* 獲取 展訊 神機的雙卡 IMSI、IMSI 資訊
*/
public static TeleInfo getSpreadtrumTeleInfo(Context context) {
TeleInfo teleInfo = new TeleInfo();
try {
TelephonyManager tm1 = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imsi_1 = tm1.getSubscriberId();
String imei_1 = tm1.getDeviceId();
int phoneType_1 = tm1.getPhoneType();
teleInfo.imsi_1 = imsi_1;
teleInfo.imei_1 = imei_1;
teleInfo.phoneType_1 = phoneType_1;
Class<?> phoneFactory = Class.forName("com.android.internal.telephony.PhoneFactory");
Method getServiceName = phoneFactory.getMethod("getServiceName", String.class, int.class);
getServiceName.setAccessible(true);
String spreadTmService = (String) getServiceName.invoke(phoneFactory, Context.TELEPHONY_SERVICE, 1);
TelephonyManager tm2 = (TelephonyManager) context.getSystemService(spreadTmService);
String imsi_2 = tm2.getSubscriberId();
String imei_2 = tm2.getDeviceId();
int phoneType_2 = tm2.getPhoneType();
teleInfo.imsi_2 = imsi_2;
teleInfo.imei_2 = imei_2;
teleInfo.phoneType_2 = phoneType_2;
} catch (Exception e) {
e.printStackTrace();
}
return teleInfo;
}
/**
* 獲取 MAC 地址
* <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
*/
public static String getMacAddress(Context context) {
//wifi mac地址
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String mac = info.getMacAddress();
return mac;
}
/**
* 獲取 開機時間
*/
public static String getBootTimeString() {
long ut = SystemClock.elapsedRealtime() / 1000;
int h = (int) ((ut / 3600));
int m = (int) ((ut / 60) % 60);
return h + ":" + m;
}
}
儲存和訪問SharedPreferences工具
package cn.studyou.baselibrary.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/**
* 基本功能:儲存和訪問SharedPreferences
* 建立:王傑
* 建立時間:16/3/7
* 郵箱:[email protected]
*/
public class OperatingSharedPreferences {
/**
* <pre>
* 基本功能:儲存String型別資料到SharedPreferences
* 編寫:王傑
*
* @param context
* @param name
* @param key
* @param value </pre>
*/
public static void setString(Context context,
String name, String key, String value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
name, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();// 獲取編輯器
editor.putString(key, value);
editor.commit();// 提交修改
}
/**
* <pre>
* 基本功能:取得SharedPreferences中儲存的String型別資料
*