为了实现一个功能,需要搜集手机信息,自己先在网上找了相关信息进行了汇总,主要是汇集手机的信息,一般想要的信息在手机设置->关于手机->状态消息里面包含了手机的各种信息,下面的代码中也主要显示了那些信息,但是源码的方法我还没有看,先把总结的贴出来。先上图(太多就截取几个)。
上代码啦,太多了,就写主要代码了。
-
- public void getSDCardInfo() {
-
-
-
- if (Environment.getExternalStorageState().equals(
- Environment.MEDIA_MOUNTED)) {
-
- File path = Environment.getExternalStorageDirectory();
- StatFs statfs = new StatFs(path.getPath());
-
- long blocSize = statfs.getBlockSize();
-
- long totalBlocks = statfs.getBlockCount();
-
- long availaBlock = statfs.getAvailableBlocks();
-
-
- long availableSize = blocSize * availaBlock;
-
-
- long allSize = blocSize * totalBlocks;
- }
-
- }
-
- public 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()) {
- Toast.makeText(this,
- inetAddress.getHostAddress().toString(), 3000)
- .show();
- return inetAddress.getHostAddress().toString();
- }
- }
- }
- } catch (SocketException ex) {
- ex.printStackTrace();
-
- }
- return "";
- }
-
-
-
-
- public String getLocalIpAddress2() {
-
- WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
-
- if (!wifiManager.isWifiEnabled()) {
- wifiManager.setWifiEnabled(true);
- }
- WifiInfo wifiInfo = wifiManager.getConnectionInfo();
- int ipAddress = wifiInfo.getIpAddress();
- String ip = intToIp(ipAddress);
- return ip;
- }
-
- private String intToIp(int i) {
- return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)
- + "." + (i >> 24 & 0xFF);
- }
-
- public String GetNetIp(String ipaddr) {
- URL infoUrl = null;
- InputStream inStream = null;
- try {
- infoUrl = new URL(ipaddr);
- URLConnection connection = infoUrl.openConnection();
- HttpURLConnection httpConnection = (HttpURLConnection) connection;
- int responseCode = httpConnection.getResponseCode();
- if (responseCode == HttpURLConnection.HTTP_OK) {
- inStream = httpConnection.getInputStream();
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(inStream, "utf-8"));
- StringBuilder strber = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null)
- strber.append(line + "\n");
- inStream.close();
- return strber.toString();
- }
- } catch (MalformedURLException e) {
-
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- return "";
- }
-
- private String getMacAddress() {
- String result = "";
- WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
- WifiInfo wifiInfo = wifiManager.getConnectionInfo();
- result = wifiInfo.getMacAddress();
-
- return result;
- }
-
- private String getWeithAndHeight() {
-
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- int width = dm.widthPixels;
- int height = dm.heightPixels;
- float density = dm.density;
- int densityDpi = dm.densityDpi;
-
- WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
- width = mWindowManager.getDefaultDisplay().getWidth();
- height = mWindowManager.getDefaultDisplay().getHeight();
-
-
- Toast msg = Toast.makeText(this, "宽=" + width + " 高=" + height,
- Toast.LENGTH_LONG);
- msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
- msg.getYOffset() / 2);
- msg.show();
- return "(像素)宽:" + width + "\n" + "(像素)高:" + height + "\n"
- + "屏幕密度(0.75 / 1.0 / 1.5):" + density + "\n"
- + "屏幕密度DPI(120 / 160 / 240):" + densityDpi + "\n";
-
- }
-
- public void getLocation() {
-
- LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
-
- LocationListener myGPSListener = new LocationListener() {
-
- public void onLocationChanged(Location location) {
- double latitude = location.getLatitude();
- double longitude = location.getLongitude();
- }
-
- public void onStatusChanged(String provider, int status,
- Bundle extras) {
- }
-
- public void onProviderEnabled(String provider) {
- }
-
- public void onProviderDisabled(String provider) {
- }
- };
-
- Criteria criteria = new Criteria();
- criteria.setAccuracy(Criteria.ACCURACY_COARSE);
- criteria.setAltitudeRequired(false);
- criteria.setCostAllowed(true);
- criteria.setPowerRequirement(Criteria.POWER_LOW);
- String provider = locationManager.getBestProvider(criteria, true);
-
- locationManager
- .requestLocationUpdates(provider, 5000, 0, myGPSListener);
-
- locationManager.removeUpdates(myGPSListener);
-
- }
-
- public boolean isNetConnecting() {
- ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
- if (networkInfo == null || !networkInfo.isConnected()) {
-
- return false;
- } else {
-
- return true;
- }
- }
-
- public void getPhoneState() {
-
- TelephonyManager telephonyManager = (TelephonyManager)
-
- PhoneStateListener MyPhoneListener = new PhoneStateListener() {
- @Override
- public void onCellLocationChanged(CellLocation location) {
- if (location instanceof GsmCellLocation) {
- int CID = ((GsmCellLocation) location).getCid();
- } else if (location instanceof CdmaCellLocation) {
- int ID = ((CdmaCellLocation) location).getBaseStationId();
- }
- }
-
- @Override
- public void onServiceStateChanged(ServiceState serviceState) {
- super.onServiceStateChanged(serviceState);
- }
-
- @Override
- public void onSignalStrengthsChanged(SignalStrength signalStrength) {
- int asu = signalStrength.getGsmSignalStrength();
- int dbm = -113 + 2 * asu;
- super.onSignalStrengthsChanged(signalStrength);
- }
- };
-
- telephonyManager.listen(MyPhoneListener,
- PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
-
-
- }
-
- private String getSystemMemory() {
-
- String availMemory = getAvailMemory();
- String totalMemory = getTotalMemory();
- return "可用内存=" + availMemory + "\n" + "总内存=" + totalMemory;
-
- }
-
- private String getAvailMemory() {
- ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
- MemoryInfo mi = new MemoryInfo();
- am.getMemoryInfo(mi);
-
- return Formatter.formatFileSize(getBaseContext(), mi.availMem);
- }
-
- private String getTotalMemory() {
- 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();
-
- arrayOfString = str2.split("\\s+");
- for (String num : arrayOfString) {
- Log.i(str2, num + "\t");
- }
-
- initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;
- localBufferedReader.close();
-
- } catch (IOException e) {
- }
- return Formatter.formatFileSize(getBaseContext(), initial_memory);
- }
-
- private String getCpuInfo() {
- String str1 = "/proc/cpuinfo";
- String str2 = "";
- String[] cpuInfo = { "", "" };
- 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 "1-cpu型号:" + cpuInfo[0] + "2-cpu频率:" + cpuInfo[1];
- }
-
- ublic String getSimCardInfo() {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- TelephonyManager tm = (TelephonyManager) this
- .getSystemService(TELEPHONY_SERVICE);
-
- tm.getCallState();
-
-
-
- CellLocation location = tm.getCellLocation();
-
-
- location.requestLocationUpdate();
-
-
- tm.getDataActivity();
-
-
- tm.getDataState();
-
-
- String Imei = tm.getDeviceId();
-
-
- tm.getDeviceSoftwareVersion();
-
-
- String phoneNum = tm.getLine1Number();
-
-
- tm.getNetworkCountryIso();
-
-
- tm.getNetworkOperator();
-
-
-
- tm.getNetworkOperatorName();
-
-
- tm.getNetworkType();
-
-
- tm.getPhoneType();
-
-
- tm.getSimCountryIso();
-
-
- tm.getSimOperator();
-
-
- tm.getSimOperatorName();
-
-
- tm.getSimSerialNumber();
-
-
- tm.getSimState();
-
-
- tm.getSubscriberId();
-
-
-
- tm.getVoiceMailAlphaTag();
-
-
- tm.getVoiceMailNumber();
-
-
- tm.hasIccCard();
-
-
- tm.isNetworkRoaming();
-
- String ProvidersName = null;
-
- String IMSI = tm.getSubscriberId();
-
- System.out.println(IMSI);
- if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
- ProvidersName = "中国移动";
- } else if (IMSI.startsWith("46001")) {
-
- ProvidersName = "中国联通";
-
- } else if (IMSI.startsWith("46003")) {
-
- ProvidersName = "中国电信";
-
- }
-
-
- List<NeighboringCellInfo> infos = tm.getNeighboringCellInfo();
- for (NeighboringCellInfo info : infos) {
-
- int cid = info.getCid();
-
-
- info.getLac();
- info.getNetworkType();
- info.getPsc();
-
- info.getRssi();
- }
- return "手机号码:" + phoneNum + "\n" + "服务商:" + ProvidersName+"\n" + "IMEI:" + Imei;
-
- }
-
-
- public void showDialog(String title, String info) {
- AlertDialog dialog = new AlertDialog.Builder(this)
- .setTitle(title)
- .setMessage(info)
- .setPositiveButton("close",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
- dialog.cancel();
- dialog = null;
- }
- }).create();
- dialog.show();
- }
本文出自 “java之路” 博客,请务必保留此出处http://2402766.blog.51cto.com/2392766/1080837
【小功能2】android获取手机信息(号码,内存,CPU,分辨率,MAC,IP,SD卡,IMEI,经纬度,信号强度等等),布布扣,bubuko.com
【小功能2】android获取手机信息(号码,内存,CPU,分辨率,MAC,IP,SD卡,IMEI,经纬度,信号强度等等)
原文:http://www.cnblogs.com/kevincode/p/3824542.html