Android獲取手機(jī)能獲取的信息(暫時(shí)我能想到的)

總結(jié)了網(wǎng)上的一些工具類,希望對(duì)大家有所幫助,大家可以在評(píng)論下方補(bǔ)全更多的獲取方法,更多的幫助大家。
原文鏈接:http://blog.csdn.net/jersey_me/article/details/71403784
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 {
/**
* 獲取聯(lián)系人
*
* @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);
// 取得聯(lián)系人名字
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, //系統(tǒng)方式獲取通訊錄存儲(chǔ)地址
            new String[]{
                    CallLog.Calls.CACHED_NAME,  //姓名
                    CallLog.Calls.NUMBER,    //號(hào)碼
                    CallLog.Calls.TYPE,  //呼入/呼出(2)/未接
                    CallLog.Calls.DATE,  //撥打時(shí)間
                    CallLog.Calls.DURATION   //通話時(shí)長
            }, 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;
            }
            //撥打時(shí)間
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date callDate = new Date(Long.parseLong(cs.getString(3)));
            String callDateStr = sdf.format(callDate);
            //通話時(shí)長
            int callDuration = Integer.parseInt(cs.getString(4));
            int min = callDuration / 60;
            int sec = callDuration % 60;
            String callDurationStr = min + "分" + sec + "秒";
            String callOne = "類型:" + callTypeStr + ", 稱呼:" + callName + ", 號(hào)碼:"
                    + callNumber + ", 通話時(shí)長:" + callDurationStr + ", 時(shí)間:" + 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");      // 獲取手機(jī)內(nèi)部短信

        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 = "發(fā)送";
                } 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手機(jī)號(hào)關(guān)聯(lián)Contacts聯(lián)系人的顯示名字
 *
 * @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 手機(jī)號(hào)過濾
    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:獲取設(shè)備基板名稱
 * android.os.Build.BOOTLOADER:獲取設(shè)備引導(dǎo)程序版本號(hào)
 * android.os.Build.BRAND:獲取設(shè)備品牌
 * android.os.Build.CPU_ABI:獲取設(shè)備指令集名稱(CPU的類型)
 * android.os.Build.CPU_ABI2:獲取第二個(gè)指令集名稱
 * android.os.Build.DEVICE:獲取設(shè)備驅(qū)動(dòng)名稱
 * android.os.Build.DISPLAY:獲取設(shè)備顯示的版本包(在系統(tǒng)設(shè)置中顯示為版本號(hào))和ID一樣
 * android.os.Build.FINGERPRINT:設(shè)備的唯一標(biāo)識(shí)。由設(shè)備的多個(gè)信息拼接合成。
 * android.os.Build.HARDWARE:設(shè)備硬件名稱,一般和基板名稱一樣(BOARD)
 * android.os.Build.HOST:設(shè)備主機(jī)地址
 * android.os.Build.ID:設(shè)備版本號(hào)。
 * android.os.Build.MODEL :獲取手機(jī)的型號(hào) 設(shè)備名稱。
 * android.os.Build.MANUFACTURER:獲取設(shè)備制造商
 * android:os.Build.PRODUCT:整個(gè)產(chǎn)品的名稱
 * android:os.Build.RADIO:無線電固件版本號(hào),通常是不可用的 顯示unknown
 * android.os.Build.TAGS:設(shè)備標(biāo)簽。如release-keys 或測試的 test-keys
 * android.os.Build.TIME:時(shí)間
 * android.os.Build.TYPE:設(shè)備版本類型主要為”user” 或”eng”.
 * android.os.Build.USER:設(shè)備用戶名 基本上都為android-build
 * android.os.Build.VERSION.RELEASE:獲取系統(tǒng)版本字符串。如4.1.2 或2.2 或2.3等
 * android.os.Build.VERSION.CODENAME:設(shè)備當(dāng)前的系統(tǒng)開發(fā)代號(hào),一般使用REL代替
 * android.os.Build.VERSION.INCREMENTAL:系統(tǒng)源代碼控制值,一個(gè)數(shù)字或者git hash值
 * android.os.Build.VERSION.SDK:系統(tǒng)的API級(jí)別 一般使用下面大的SDK_INT 來查看
 * android.os.Build.VERSION.SDK_INT:系統(tǒng)的API級(jí)別 數(shù)字表示
 *
 * @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; // 手機(jī)型號(hào)
    String numer = mTm.getLine1Number(); // 手機(jī)號(hào)碼,有的可得,有的不可得
    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("手機(jī)型號(hào)--" + mtype + '\n')
            .append("本機(jī)號(hào)碼--" + numer + '\n')
            .append("屏幕分辨率--" + getScreen(context) + '\n')
            .append("藍(lán)牙MAC地址--" + m_szBTMAC + '\n')
            .append("WIFI MAC地址--" + macAddress + '\n')
            .append("序列號(hào)--" + sn + '\n')
            .append("手機(jī)廠商--" + android.os.Build.MANUFACTURER.replace(" ", "-") + '\n')
            .append("DEVICEID--" + Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID) + '\n')
            .append("手機(jī)版本號(hào)--" + android.os.Build.VERSION.RELEASE + '\n')
            .append("cpu最小頻率--" + getCpuInfo()[1] + '\n' + "當(dāng)前頻率--" + getCurCpuFreq() + '\n')
            .append("最大頻率--" + getMaxCpuFreq() + '\n')
            .append("最大內(nèi)存--" + getTotalMemory(context) + '\n')
            .append("可用內(nèi)存--" + getAvailMemory(context) + '\n')
            .append("聯(lián)網(wǎng)狀態(tài)--" + (isWifi(context) ? "wifi" : "運(yùn)營商") + '\n')
    ;


    return sb.toString();
}


public static String getPhoneInfo(Context context) {

    StringBuilder phoneInfo = new StringBuilder();
    phoneInfo.append("產(chǎn)品名稱: " + android.os.Build.PRODUCT + System.getProperty("line.separator"));
    phoneInfo.append("CPU_名稱: " + android.os.Build.CPU_ABI + System.getProperty("line.separator"));
    phoneInfo.append("設(shè)備標(biāo)簽: " + 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("手機(jī)名稱: " + android.os.Build.BRAND + System.getProperty("line.separator"));
    phoneInfo.append("設(shè)備基板名稱: " + android.os.Build.BOARD + System.getProperty("line.separator"));
    phoneInfo.append("設(shè)備的唯一標(biāo)識(shí): " + android.os.Build.FINGERPRINT + System.getProperty("line.separator"));
    phoneInfo.append("設(shè)備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("運(yùn)營商 = " + tm.getNetworkOperatorName() + System.getProperty("line.separator"));
    phoneInfo.append("手機(jī)類型 = " + 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;
}


/**
 * 手機(jī)CPU信息
 */
private static String[] getCpuInfo() {
    String str1 = "/proc/cpuinfo";
    String str2 = "";
    String[] cpuInfo = {"", ""};  //1-cpu型號(hào)  //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;
}

/**
 * 獲取手機(jī)內(nèi)存大小
 *
 * @return
 */
public static String getTotalMemory(Context context) {
    String str1 = "/proc/meminfo";// 系統(tǒng)內(nèi)存信息文件
    String str2;
    String[] arrayOfString;
    long initial_memory = 0;
    try {
        FileReader localFileReader = new FileReader(str1);
        BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
        str2 = localBufferedReader.readLine();// 讀取meminfo第一行,系統(tǒng)總內(nèi)存大小

        arrayOfString = str2.split("\\s+");
        for (String num : arrayOfString) {
            Log.i(str2, num + "\t");
        }

        initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 獲得系統(tǒng)總內(nèi)存,單位是KB,乘以1024轉(zhuǎn)換為Byte
        localBufferedReader.close();

    } catch (IOException e) {
    }
    return Formatter.formatFileSize(context, initial_memory);// Byte轉(zhuǎn)換為KB或者M(jìn)B,內(nèi)存大小規(guī)格化
}

/**
 * 獲取當(dāng)前可用內(nèi)存大小
 *
 * @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";
}


// 實(shí)時(shí)獲取CPU當(dāng)前頻率(單位KHZ)

public 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;
}

/**
 * 網(wǎng)絡(luò)狀態(tài)
 *
 * @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;
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容