1. 程式人生 > >獲取手機的相關資訊

獲取手機的相關資訊

  1. package com.ransj.tool;
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream.GetField;
  7. import android.Manifest;
  8. import android.app.Activity;
  9. import android.app.ActivityManager;
  10. import android.app.ActivityManager.MemoryInfo;
  11. import android.content.Context;
  12. import android.content.pm.PackageManager;
  13. import android.net.ConnectivityManager;
  14. import android.net.NetworkInfo;
  15. import android.os.Build;
  16. import android.telephony.TelephonyManager;
  17. import android.util.Log;
  18. /**
  19. * retrieve phone info
  20. *

  21. */
  22. public class PhoneInfo {
  23.         private static final String TAG = PhoneInfo.class.getSimpleName();
  24.         private static final String FILE_MEMORY = "/proc/meminfo";
  25.         private static final String FILE_CPU = "/proc/cpuinfo";
  26.         public String mIMEI;
  27.         public int mPhoneType;
  28.         public int mSysVersion;
  29.         public String mNetWorkCountryIso;
  30.         public String mNetWorkOperator;
  31.         public String mNetWorkOperatorName;
  32.         public int mNetWorkType;
  33.         public boolean mIsOnLine;
  34.         public String mConnectTypeName;
  35.         public long mFreeMem;
  36.         public long mTotalMem;
  37.         public String mCupInfo;
  38.         public String mProductName;
  39.         public String mModelName;
  40.         public String mManufacturerName;
  41.         /**
  42.          * private constructor
  43.          */
  44.         private PhoneInfo() {
  45.         }
  46.         /**
  47.          * get imei
  48.          * 
  49.          * @return
  50.          */
  51.         public static String getIMEI(Context context) {
  52.                 TelephonyManager manager = (TelephonyManager) context
  53.                                 .getSystemService(Activity.TELEPHONY_SERVICE);
  54.                 // check if has the permission
  55.                 if (PackageManager.PERMISSION_GRANTED == context.getPackageManager()
  56.                                 .checkPermission(Manifest.permission.READ_PHONE_STATE,
  57.                                                 context.getPackageName())) {
  58.                         return manager.getDeviceId();
  59.                 } else {
  60.                         return null;
  61.                 }
  62.         }
  63.         /**
  64.          * get phone type,like :GSM��CDMA��SIP��NONE
  65.          * 
  66.          * @param context
  67.          * @return
  68.          */
  69.         public static int getPhoneType(Context context) {
  70.                 TelephonyManager manager = (TelephonyManager) context
  71.                                 .getSystemService(Activity.TELEPHONY_SERVICE);
  72.                 return manager.getPhoneType();
  73.         }
  74.         /**
  75.          * get phone sys version
  76.          * 
  77.          * @return
  78.          */
  79.         public static int getSysVersion() {
  80.                 return Build.VERSION.SDK_INT;
  81.         }
  82.         /**
  83.          * Returns the ISO country code equivalent of the current registered
  84.          * operator's MCC (Mobile Country Code).
  85.          * 
  86.          * @param context
  87.          * @return
  88.          */
  89.         public static String getNetWorkCountryIso(Context context) {
  90.                 TelephonyManager manager = (TelephonyManager) context
  91.                                 .getSystemService(Activity.TELEPHONY_SERVICE);
  92.                 return manager.getNetworkCountryIso();
  93.         }
  94.         /**
  95.          * Returns the numeric name (MCC+MNC) of current registered operator.may not
  96.          * work on CDMA phone
  97.          * 
  98.          * @param context
  99.          * @return
  100.          */
  101.         public static String getNetWorkOperator(Context context) {
  102.                 TelephonyManager manager = (TelephonyManager) context
  103.                                 .getSystemService(Activity.TELEPHONY_SERVICE);
  104.                 return manager.getNetworkOperator();
  105.         }
  106.         /**
  107.          * Returns the alphabetic name of current registered operator.may not work
  108.          * on CDMA phone
  109.          * 
  110.          * @param context
  111.          * @return
  112.          */
  113.         public static String getNetWorkOperatorName(Context context) {
  114.                 TelephonyManager manager = (TelephonyManager) context
  115.                                 .getSystemService(Activity.TELEPHONY_SERVICE);
  116.                 return manager.getNetworkOperatorName();
  117.         }
  118.         /**
  119.          * get type of current network
  120.          * 
  121.          * @param context
  122.          * @return
  123.          */
  124.         public static int getNetworkType(Context context) {
  125.                 TelephonyManager manager = (TelephonyManager) context
  126.                                 .getSystemService(Activity.TELEPHONY_SERVICE);
  127.                 return manager.getNetworkType();
  128.         }
  129.         /**
  130.          * is webservice aviliable
  131.          * 
  132.          * @param context
  133.          * @return
  134.          */
  135.         public static boolean isOnline(Context context) {
  136.                 ConnectivityManager manager = (ConnectivityManager) context
  137.                                 .getSystemService(Activity.CONNECTIVITY_SERVICE);
  138.                 NetworkInfo info = manager.getActiveNetworkInfo();
  139.                 if (info != null && info.isConnected()) {
  140.                         return true;
  141.                 }
  142.                 return false;
  143.         }
  144.         /**
  145.          * get current data connection type name ,like ,Mobile��WIFI��OFFLINE
  146.          * 
  147.          * @param context
  148.          * @return
  149.          */
  150.         public static String getConnectTypeName(Context context) {
  151.                 if (!isOnline(context)) {
  152.                         return "OFFLINE";
  153.                 }
  154.                 ConnectivityManager manager = (ConnectivityManager) context
  155.                                 .getSystemService(Activity.CONNECTIVITY_SERVICE);
  156.                 NetworkInfo info = manager.getActiveNetworkInfo();
  157.                 if (info != null) {
  158.                         return info.getTypeName();
  159.                 } else {
  160.                         return "OFFLINE";
  161.                 }
  162.         }
  163.         /**
  164.          * get free memory of phone, in M
  165.          * 
  166.          * @param context
  167.          * @return
  168.          */
  169.         public static long getFreeMem(Context context) {
  170.                 ActivityManager manager = (ActivityManager) context
  171.                                 .getSystemService(Activity.ACTIVITY_SERVICE);
  172.                 MemoryInfo info = new MemoryInfo();
  173.                 manager.getMemoryInfo(info);
  174.                 long free = info.availMem / 1024 / 1024;
  175.                 return free;
  176.         }
  177.         /**
  178.          * get total memory of phone , in M
  179.          * 
  180.          * @param context
  181.          * @return
  182.          */
  183.         public static long getTotalMem(Context context) {
  184.                 try {
  185.                         FileReader fr = new FileReader(FILE_MEMORY);
  186.                         BufferedReader br = new BufferedReader(fr);
  187.                         String text = br.readLine();
  188.                         String[] array = text.split("\\s+");
  189.                         Log.w(TAG, text);
  190.                         return Long.valueOf(array[1]) / 1024;
  191.                 } catch (FileNotFoundException e) {
  192.                         e.printStackTrace();
  193.                 } catch (IOException e) {
  194.                         e.printStackTrace();
  195.                 }
  196.                 return -1;
  197.         }
  198.         public static String getCpuInfo() {
  199.                 try {
  200.                         FileReader fr = new FileReader(FILE_CPU);
  201.                         BufferedReader br = new BufferedReader(fr);
  202.                         String text = br.readLine();
  203.                         String[] array = text.split(":\\s+", 2);
  204.                         for (int i = 0; i < array.length; i++) {
  205.                                 Log.w(TAG, " .....  " + array[i]);
  206.                         }
  207.                         Log.w(TAG, text);
  208.                         return array[1];
  209.                 } catch (FileNotFoundException e) {
  210.                         e.printStackTrace();
  211.                 } catch (IOException e) {
  212. 相關推薦

    使用gethostname()函式和gethostbyname()函式獲取主機相關資訊

    gethostname():返回本地主機的標準主機名 原型如下: #include <unistd.h> int gethostname(char *name, size_t len); 引數說明: 這個函式需要兩個引數: 接收緩衝區name,其長度必須為len位元組或是更長

    Android 獲取手機儲存資訊詳解(記憶體,外存等)

    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); //系統記憶體資訊 ActivityManager.MemoryInfo memInfo = new ActivityManager

    JS獲取 URL相關資訊,引數

    Location 物件包含有關當前 URL 的資訊。 Location 物件是 Window 物件的一個部分,可通過 window.location 屬性來訪問。 hash 設定或返回從井號 (

    請教一個能在WinPE環境下獲取系統相關資訊的程式碼

    來上海將近2年了,然而我已經換過了3家公司,也許很多人都很懵逼,這也太頻繁了吧!你有啥不滿足的?我想如果是一個面試官,一聽2年內換3家 ,肯定是不想要這個人的。所以我在面試第四家的時候,我就把我第三家的工作經歷去掉了,沒寫第三家,也許有人會站在道德的制高點說我了,說我造假什麼

    iOS開發-Object-C獲取手機裝置資訊(UIDevice)

    一、獲取UiDevice裝置資訊 // 獲取裝置名稱 NSString *name = [[UIDevice currentDevice] name]; // 獲取裝置系統名稱 NSString *systemName = [[UIDevice currentDevice] systemName

    android-獲取手機小區資訊介面設計

    1. manifests <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package

    獲取數字證書相關資訊,證書有效性驗證,RSA加密和解密功能之獲取證書相關資訊

    //公鑰 private PublicKey pk; /** 後臺將證書以byte陣列的形式傳入 * @param bytes,數字證書crt傳入的byte陣列 * @return */ public InfoEntity getCertificateInfo(

    Android如何獲取手機各項資訊

    1、使用Build獲取架構屬性 下面我們來根據原始碼看看通過Build這個類可以得到哪些配置資訊,具體就不解釋了,從命名基本可以理解其代表的屬性。 public class Build { //當一個版本屬性不知道時所設定的值。 publ

    ABPeoplePickerNavigationController 獲取手機通訊錄資訊

    最近專案用到獲取使用者手機通訊錄的功能,但發現有兩個代理方法在iOS 9.0 廢棄了,用新的代理方法代替,所以整理下供以後參考。。 #pragma mark - 點選聯絡人 連結到使用者手機通訊錄 - (void)accessPhoneBook: (UIButton *)

    Android使用NotificationListenerService獲取通知相關資訊

    前言 我們可能需要監聽通知欄,來獲取系統通知,然後去得到通知的相關資訊,或者我們可能需要知道某一個通知是否處於活動狀態還是已經被使用者處理了! 概況 在Android4.4之前,我們更多的是通過反射來獲取通知的相關資訊!NotificationList

    原生js獲取手機定位資訊

    <script type="text/javascript"> function Location() {}; Location.prototype.getLocation = function (callback) { var options = {

    獲取手機聯絡人資訊 很簡單的方法

    String a; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.la

    獲取裝置相關資訊工具包

    拿走不謝 在使用springboot獲取螢幕引數時,可能會報關於headless為NULL的錯誤,不要慌,這樣來:只需要在啟動的application中替換main函式內容為: @SpringBootApplication public class Ad

    android--------根據檔案路徑使用File類獲取檔案相關資訊

    Android通過檔案路徑如何得到檔案相關資訊,如 檔名稱,檔案大小,建立時間,檔案的相對路徑,檔案的絕對路徑等。如圖:public class MainActivity extends Activity { private String path = "/storage

    Android獲取手機基站資訊並進行基站定位(基站定位原理)

    http://blog.csdn.net/mad1989/article/details/9970431 一,首先普及一下手機基站資訊中相關的專業詞彙:  通過TelephonyManager 獲取lac:mcc:mnc:cell-id(基站資訊)的解釋: MCC,M

    Android 獲取手機儲存資訊詳解(記憶體,外存等)

            android  獲取手機儲存資訊詳解(記憶體,外存等)         android不像ios,android可以外接Sd卡,並且也會有內接的儲存卡,此次專門研究下如何獲取android的儲存資訊。 一、RAM記憶體         RAM,也就是我們常說的手機記憶體。最早的記憶體大小

    iOS獲取裝置相關資訊

    UIDevice *device = [UIDevicenew]; NSLog(@"%@ %@ %@ %@ %@",device.name,device.model,device.localizedModel,device.systemName,device.syste

    獲取手機品牌資訊的Build類

    一、需求 Build類獲取手機制造商,系統定製商,型號,Android 系統版本,CPU 指令集,可以檢視是否支援64位等 二、原始碼 // // Source code recreated from a .class file by Intell

    如何使用ADB命令檢視Android手機相關資訊引數

      本文主要介紹的是教大家如何使用ADB命令來檢視自己Android手機的相關硬體以及其他的引數資訊,相信許多機友已經早已檢視過,而新入門感興趣的機友可以嘗試一下…… 本文主要介紹的是教大家如何使用ADB命令來檢視自己手機的相關硬體以及其他的引數資訊,相信許多機友已經早

    Android ContentResolver ContactsContract 獲取手機聯絡人資訊

    獲取手機聯絡人資訊步驟: 1、獲取 ContentResolver ContentResolver resolver = getContentResolver(); 2、resolver.query(*)查詢資訊 查詢手機聯絡人的URI:Cont