1. 程式人生 > >Android獲取手機能獲取的資訊(暫時我能想到的)

Android獲取手機能獲取的資訊(暫時我能想到的)

總結了網上的一些工具類,希望對大家有所幫助,大家可以在評論下方補全更多的獲取方法,更多的幫助大家,謝謝。
package com.mydemo.utils;
import android.Manifest;
import android.app.ActivityManager;
import android.bluetooth.BluetoothAdapter;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import 
android.database.Cursor; import android.database.sqlite.SQLiteException; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.net.wifi.WifiManager; import android.provider.CallLog; import android.provider.ContactsContract; import android.provider.Settings;
import android.support.v4.app.ActivityCompat; import android.telephony.TelephonyManager; import android.text.format.Formatter; import android.util.DisplayMetrics; import android.util.Log; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;
import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import static android.content.Context.TELEPHONY_SERVICE; /** * Created by JerseyGuo * on 2015/5/5. */ public class Utils { /** * 獲取聯絡人 * * @param context * @return */ public static List<String> queryContactPhoneNumber(Context context) { List<String> infos = new ArrayList<>(); String[] cols = {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}; Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, cols, null, null, null); for (int i = 0; i < cursor.getCount(); i++) { cursor.moveToPosition(i); // 取得聯絡人名字 int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME); int numberFieldColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); String name = cursor.getString(nameFieldColumnIndex); String number = cursor.getString(numberFieldColumnIndex); infos.add(name + number); } return infos; } /** * 獲取通話記錄 * * @param context * @return */ public static String getCallHistoryList(Context context) { ContentResolver cr = context.getContentResolver(); Cursor cs; cs = cr.query(CallLog.Calls.CONTENT_URI, //系統方式獲取通訊錄儲存地址 new String[]{ CallLog.Calls.CACHED_NAME, //姓名 CallLog.Calls.NUMBER, //號碼 CallLog.Calls.TYPE, //呼入/撥出(2)/未接 CallLog.Calls.DATE, //撥打時間 CallLog.Calls.DURATION //通話時長 }, null, null, CallLog.Calls.DEFAULT_SORT_ORDER); String callHistoryListStr = ""; int i = 0; if (cs != null && cs.getCount() > 0) { for (cs.moveToFirst(); !cs.isAfterLast() & i < 50; cs.moveToNext()) { String callName = cs.getString(0); String callNumber = cs.getString(1); //通話型別 int callType = Integer.parseInt(cs.getString(2)); String callTypeStr = ""; switch (callType) { case CallLog.Calls.INCOMING_TYPE: callTypeStr = "呼入"; break; case CallLog.Calls.OUTGOING_TYPE: callTypeStr = "撥出"; break; case CallLog.Calls.MISSED_TYPE: callTypeStr = "未接"; break; } //撥打時間 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date callDate = new Date(Long.parseLong(cs.getString(3))); String callDateStr = sdf.format(callDate); //通話時長 int callDuration = Integer.parseInt(cs.getString(4)); int min = callDuration / 60; int sec = callDuration % 60; String callDurationStr = min + "" + sec + ""; String callOne = "型別:" + callTypeStr + ", 稱呼:" + callName + ", 號碼:" + callNumber + ", 通話時長:" + callDurationStr + ", 時間:" + callDateStr + "\n---------------------\n"; callHistoryListStr += callOne; i++; } } return callHistoryListStr; } /** * 獲取簡訊 * * @param context * @return */ public static String getSmsInPhone(Context context) { final String SMS_URI_ALL = "content://sms/"; final String SMS_URI_INBOX = "content://sms/inbox"; final String SMS_URI_SEND = "content://sms/sent"; final String SMS_URI_DRAFT = "content://sms/draft"; final String SMS_URI_OUTBOX = "content://sms/outbox"; final String SMS_URI_FAILED = "content://sms/failed"; final String SMS_URI_QUEUED = "content://sms/queued"; StringBuilder smsBuilder = new StringBuilder(); try { Uri uri = Uri.parse(SMS_URI_ALL); String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"}; Cursor cur = context.getContentResolver().query(uri, projection, null, null, "date desc"); // 獲取手機內部簡訊 if (cur.moveToFirst()) { int index_Address = cur.getColumnIndex("address"); int index_Person = cur.getColumnIndex("person"); int index_Body = cur.getColumnIndex("body"); int index_Date = cur.getColumnIndex("date"); int index_Type = cur.getColumnIndex("type"); do { String strAddress = cur.getString(index_Address); int intPerson = cur.getInt(index_Person); String strbody = cur.getString(index_Body); long longDate = cur.getLong(index_Date); int intType = cur.getInt(index_Type); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date d = new Date(longDate); String strDate = dateFormat.format(d); String strType = ""; if (intType == 1) { strType = "接收"; } else if (intType == 2) { strType = "傳送"; } else { strType = "null"; } smsBuilder.append("[ "); smsBuilder.append(strAddress + ", "); smsBuilder.append(intPerson + ", "); smsBuilder.append(strbody + ", "); smsBuilder.append(strDate + ", "); smsBuilder.append(strType); smsBuilder.append(" ]\n\n"); } while (cur.moveToNext()); if (!cur.isClosed()) { cur.close(); cur = null; } } else { smsBuilder.append("no result!"); } // end if smsBuilder.append("getSmsInPhone has executed!"); } catch (SQLiteException ex) { Log.d("SQLiteException in getSmsInPhone", ex.getMessage()); } return smsBuilder.toString(); } /** * 通過address手機號關聯Contacts聯絡人的顯示名字 * * @param address * @return */ private static String getPeopleNameFromPerson(String address, Context context) { if (address == null || address == "") { return null; } String strPerson = "null"; String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}; Uri uri_Person = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address); // address 手機號過濾 Cursor cursor = context.getContentResolver().query(uri_Person, projection, null, null, null); if (cursor.moveToFirst()) { int index_PeopleName = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); String strPeopleName = cursor.getString(index_PeopleName); strPerson = strPeopleName; } else { strPerson = address; } cursor.close(); cursor = null; return strPerson; } /** * android.os.Build.BOARD:獲取裝置基板名稱 * android.os.Build.BOOTLOADER:獲取裝置載入程式版本號 * android.os.Build.BRAND:獲取裝置品牌 * android.os.Build.CPU_ABI:獲取裝置指令集名稱(CPU的型別) * android.os.Build.CPU_ABI2:獲取第二個指令集名稱 * android.os.Build.DEVICE:獲取裝置驅動名稱 * android.os.Build.DISPLAY:獲取裝置顯示的版本包(在系統設定中顯示為版本號)和ID一樣 * android.os.Build.FINGERPRINT:裝置的唯一標識。由裝置的多個資訊拼接合成。 * android.os.Build.HARDWARE:裝置硬體名稱,一般和基板名稱一樣(BOARD* android.os.Build.HOST:裝置主機地址 * android.os.Build.ID:裝置版本號。 * android.os.Build.MODEL :獲取手機的型號 裝置名稱。 * android.os.Build.MANUFACTURER:獲取裝置製造商 * android:os.Build.PRODUCT:整個產品的名稱 * android:os.Build.RADIO:無線電韌體版本號,通常是不可用的 顯示unknown * android.os.Build.TAGS:裝置標籤。如release-keys 或測試的 test-keys * android.os.Build.TIME:時間 * android.os.Build.TYPE:裝置版本型別主要為”user” ”eng”. * android.os.Build.USER:裝置使用者名稱 基本上都為android-build * android.os.Build.VERSION.RELEASE:獲取系統版本字串。如4.1.2 2.2 2.3* android.os.Build.VERSION.CODENAME:裝置當前的系統開發代號,一般使用REL代替 * android.os.Build.VERSION.INCREMENTAL:系統原始碼控制值,一個數字或者git hash* android.os.Build.VERSION.SDK:系統的API級別 一般使用下面大的SDK_INT 來檢視 * android.os.Build.VERSION.SDK_INT:系統的API級別 數字表示 * * @param context * @return */ public static String getInfo(Context context) { TelephonyManager mTm = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE); String imei = mTm.getDeviceId(); String imsi = mTm.getSubscriberId(); String mtype = android.os.Build.MODEL; // 手機型號 String numer = mTm.getLine1Number(); // 手機號碼,有的可得,有的不可得 BluetoothAdapter m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); String m_szBTMAC = m_BluetoothAdapter.getAddress(); WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); String macAddress = wm.getConnectionInfo().getMacAddress(); String sn = mTm.getSimSerialNumber(); StringBuilder sb = new StringBuilder(); sb.append("IMEI--" + imei + '\n') .append("IMSI--" + imsi + '\n') .append("手機型號--" + mtype + '\n') .append("本機號碼--" + numer + '\n') .append("螢幕解析度--" + getScreen(context) + '\n') .append("藍芽MAC地址--" + m_szBTMAC + '\n') .append("WIFI MAC地址--" + macAddress + '\n') .append("序列號--" + sn + '\n') .append("手機廠商--" + android.os.Build.MANUFACTURER.replace(" ", "-") + '\n') .append("DEVICEID--" + Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID) + '\n') .append("手機版本號--" + android.os.Build.VERSION.RELEASE + '\n') .append("cpu最小頻率--" + getCpuInfo()[1] + '\n' + "當前頻率--" + getCurCpuFreq() + '\n') .append("最大頻率--" + getMaxCpuFreq() + '\n') .append("最大記憶體--" + getTotalMemory(context) + '\n') .append("可用記憶體--" + getAvailMemory(context) + '\n') .append("聯網狀態--" + (isWifi(context) ? "wifi" : "運營商") + '\n') ; return sb.toString(); } public static String getPhoneInfo(Context context) { StringBuilder phoneInfo = new StringBuilder(); phoneInfo.append("產品名稱: " + android.os.Build.PRODUCT + System.getProperty("line.separator")); phoneInfo.append("CPU_名稱: " + android.os.Build.CPU_ABI + System.getProperty("line.separator")); phoneInfo.append("裝置標籤: " + android.os.Build.TAGS + System.getProperty("line.separator")); phoneInfo.append("VERSION_CODES.BASE: " + android.os.Build.VERSION_CODES.BASE + System.getProperty("line.separator")); phoneInfo.append("SDK版本: " + android.os.Build.VERSION.SDK + System.getProperty("line.separator")); phoneInfo.append("DEVICE: " + android.os.Build.DEVICE + System.getProperty("line.separator")); phoneInfo.append("手機名稱: " + android.os.Build.BRAND + System.getProperty("line.separator")); phoneInfo.append("裝置基板名稱: " + android.os.Build.BOARD + System.getProperty("line.separator")); phoneInfo.append("裝置的唯一標識: " + android.os.Build.FINGERPRINT + System.getProperty("line.separator")); phoneInfo.append("裝置ID: " + android.os.Build.ID + System.getProperty("line.separator")); phoneInfo.append("USER: " + android.os.Build.USER + System.getProperty("line.separator")); TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); phoneInfo.append("NetworkOperator = " + tm.getNetworkOperator() + System.getProperty("line.separator")); phoneInfo.append("運營商 = " + tm.getNetworkOperatorName() + System.getProperty("line.separator")); phoneInfo.append("手機型別 = " + tm.getPhoneType() + System.getProperty("line.separator")); phoneInfo.append("國家 = " + tm.getSimCountryIso() + System.getProperty("line.separator")); phoneInfo.append("SimState = " + tm.getSimState() + System.getProperty("line.separator")); phoneInfo.append("VoiceMailNumber = " + tm.getVoiceMailNumber() + System.getProperty("line.separator")); phoneInfo.append(getInfo(context)); return phoneInfo.toString(); } /** * 獲取解析度 */ public static String getScreen(Context context) { DisplayMetrics dm = new DisplayMetrics(); dm = context.getResources().getDisplayMetrics(); int screenWidth = dm.widthPixels; int screenHeight = dm.heightPixels; return screenWidth + "*" + screenHeight; } /** * 手機CPU資訊 */ private static String[] getCpuInfo() { String str1 = "/proc/cpuinfo"; String str2 = ""; String[] cpuInfo = {"", ""}; //1-cpu型號 //2-cpu頻率 String[] arrayOfString; try { FileReader fr = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(fr, 8192); str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); for (int i = 2; i < arrayOfString.length; i++) { cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " "; } str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); cpuInfo[1] += arrayOfString[2]; localBufferedReader.close(); } catch (IOException e) { } return cpuInfo; } /** * 獲取手機記憶體大小 * * @return */ public static String getTotalMemory(Context context) { String str1 = "/proc/meminfo";// 系統記憶體資訊檔案 String str2; String[] arrayOfString; long initial_memory = 0; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192); str2 = localBufferedReader.readLine();// 讀取meminfo第一行,系統總記憶體大小 arrayOfString = str2.split("\\s+"); for (String num : arrayOfString) { Log.i(str2, num + "\t"); } initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 獲得系統總記憶體,單位是KB,乘以1024轉換為Byte localBufferedReader.close(); } catch (IOException e) { } return Formatter.formatFileSize(context, initial_memory);// Byte轉換為KB或者MB,記憶體大小規格化 } /** * 獲取當前可用記憶體大小 * * @return */ public static String getAvailMemory(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); am.getMemoryInfo(mi); return Formatter.formatFileSize(context, mi.availMem); } /** * cpu最大頻率 * * @return */ public static String getMaxCpuFreq() { String result = ""; ProcessBuilder cmd; try { String[] args = {"/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"}; cmd = new ProcessBuilder(args); Process process = cmd.start(); InputStream in = process.getInputStream(); byte[] re = new byte[24]; while (in.read(re) != -1) { result = result + new String(re); } in.close(); } catch (IOException ex) { ex.printStackTrace(); result = "N/A"; } return result.trim() + "Hz"; } // 實時獲取CPU當前頻率(單位KHZpublic static String getCurCpuFreq() { String result = "N/A"; try { FileReader fr = new FileReader( "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"); BufferedReader br = new BufferedReader(fr); String text = br.readLine(); result = text.trim() + "Hz"; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 網路狀態 * * @param mContext * @return */ public static boolean isWifi(Context mContext) { ConnectivityManager connectivityManager = (ConnectivityManager) mContext .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { return true; } return false; } }