美文网首页
Android之获取手机信息汇总

Android之获取手机信息汇总

作者: M_天河 | 来源:发表于2020-08-28 18:17 被阅读0次

    逆向过程中有某些加密算法常常使用设备信息作为加密参数,这时需要我们把设备信息全部打出来,然后配合敏锐的洞察能力去分析。

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.InetAddress;
    import java.net.MalformedURLException;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.Enumeration;
    import java.util.List;
    import android.app.Activity;
    import android.app.ActivityManager;
    import android.app.ActivityManager.MemoryInfo;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.net.wifi.WifiInfo;
    import android.net.wifi.WifiManager;
    import android.os.Bundle;
    import android.os.Environment;
    import android.os.StatFs;
    import android.telephony.CellLocation;
    import android.telephony.NeighboringCellInfo;
    import android.telephony.PhoneStateListener;
    import android.telephony.ServiceState;
    import android.telephony.SignalStrength;
    import android.telephony.TelephonyManager;
    import android.telephony.cdma.CdmaCellLocation;
    import android.telephony.gsm.GsmCellLocation;
    import android.text.format.Formatter;
    import android.util.DisplayMetrics;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.WindowManager;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
             @Override
             protected void onCreate(Bundle savedInstanceState) {
                     super.onCreate(savedInstanceState);
                     setContentView(R.layout.activity_main);
                     Log.d("SystemInfo:", getSystemInfo());
              }
    
    private String getSystemInfo(){
          Log.i("Memory:",getSystemMemInfo());
    }
    private String getSystemMemInfo() {
    //通常系统内存信息存储在"/proc/meminfo"文件中,只需读取文件第一行即可获取手机总内存大小,可用内存通过ActivityManager.MemoryInfo获取
          String availMemory = getAvailMemory();
          String totalMemory = getTotalMemory();
          return "availMemory:" + availMemory + "\n" + "totalMemory:" + totalMemory;
    }
    // 获取android当前可用内存大小
    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) {
              Log.i("TotalMemError",Log.getStackTraceString(e));
          }
          return Formatter.formatFileSize(getBaseContext(), initial_memory);
    }
    
    获取手机ip方法1

    需要权限
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    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();
            Log.e("ifo", ex.toString());
    }
    return "";
    }
    
    获取手机ip方法2

    需要用户权限
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

    public String getLocalIpAddress2() {
          // 获取wifi服务
          WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
          // 判断wifi是否开启
          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);
    }
    
    获取手机MAC地址方法1
    private String getMacAddress() {
          String result = "";
          WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
          WifiInfo wifiInfo = wifiManager.getConnectionInfo();
          result = wifiInfo.getMacAddress();
          Log.i(TAG, "macAdd:" + result);
          return result;
    }
    
    获取手机MAC地址方法2

    读文件/sys/class/net/wlan0/address;

    获取手机MAC地址方法3
    public static String getLocalMacAddressFromIp(Context context) {
        String mac_s= "";
        try {
            byte[] mac;
            NetworkInterface ne=NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
            mac = ne.getHardwareAddress();
            mac_s = byte2hex(mac);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mac_s;
    }
    
    获取手机经纬度
    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); 
          // 选择最佳Provider
          locationManager.requestLocationUpdates(provider, 5000, 0, myGPSListener);
          // 移除LocationListener
          locationManager.removeUpdates(myGPSListener);
    }
    
    当前网络状态(是否连接)
    public boolean isNetConnecting() {
          ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
          if (networkInfo == null || !networkInfo.isConnected()) {
                // info.setConnected(false);
                return false;
    } else {
                // info.setConnected(true);
                return true;
            }
    }
    
    获取手机CPU信息方法1

    读取/proc/cpuinfo文件,第一行是型号,第二行是频率

    private String getCpuInfo() {
          String str1 = "/proc/cpuinfo";
          String str2 = "";
          String[] cpuInfo = { "", "" }; //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) {
                    e.printStackTrace();
                }
                Log.i(TAG, "cpuinfo:" + cpuInfo[0] + " " + cpuInfo[1]);
                return "cpu model:" + cpuInfo[0] + "cpu rate:" + cpuInfo[1];
    }
    
    获取手机CPU信息方法2

    直接通过api获取cpu型号

    android.os.Build.BOARD

    获取sim卡信息

    需要权限 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    public 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();//获取设备id
          tm.getDeviceSoftwareVersion();//获取软件版本
          String phoneNum = tm.getLine1Number();//获取手机号,但是目前这种方法基本都获取不到,因为手机号信息不再保存在本地
          tm.getSimCountryIso();//获取国家码
          tm.getNetworkCountryIso();//和上一个相同,具体区别不大
          tm.getNetworkType();//获取网络类型
          tm.getSimSerialNumber();//获取sim卡序列号
          tm.getSubscriberId();//获取imsi,IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。
          String IMSI = tm.getSubscriberId(); 
          String ProvidersName = null;
          if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
                ProvidersName = "中国移动";
          } else if (IMSI.startsWith("46001")) {
                ProvidersName = "中国联通";
          } else if (IMSI.startsWith("46003")) {
                ProvidersName = "中国电信";
          }      
    }
    
    获取手机中已安装apk文件信息

    通过PackageManager可获取手机端已安装的apk文件的信息,具体代码如下:

    PackageManager packageManager = this.getPackageManager(); 
    List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);
    

    通过上述方法,可得到手机中安装的所有应用程序,包括手动安装的apk包的信息、系统预装的应用软件的信息,要区分这两类软件可使用以下方法:`

    1. 从packageInfoList获取的packageInfo,再通过packageInfo.applicationInfo获取applicationInfo。
    2. 判断(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)的值,该值大于0时,表示获取的应用为系统预装的应用,反之则为手动安装的应用。`
    public static List<PackageInfo> getAllApps(Context context) {
          List<PackageInfo> apps = new ArrayList<PackageInfo>(); 
          PackageManager pManager = context.getPackageManager(); 
          //获取手机内所有应用 
          List<PackageInfo> paklist = pManager.getInstalledPackages(0); 
          for (int i = 0; i < paklist.size(); i++) { 
                PackageInfo pak = (PackageInfo) paklist.get(i);
                //判断是否为非系统预装的应用程序 
                if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
                      apps.add(pak); 
                }
          } 
    return apps;
     } 
    

    获取图片、应用名、包名:

    PackageManager pManager = MessageSendActivity.this.getPackageManager(); 
    List<PackageInfo> appList = Utils.getAllApps(MessageSendActivity.this); 
    for(int i=0;i<appList.size();i++) { 
          PackageInfo pinfo = appList.get(i); 
          appInfo = new AppInfo(); 
          appInfo.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo));
          appInfo.setLabel(pManager.getApplicationLabel(pinfo.applicationInfo).toString());  
          appInfo.setPackageName(pinfo.applicationInfo.packageName); 
    }//appInfo是自定义保存类
    

    通过PackageInfo获取具体信息方法

    包名获取方法:packageInfo.packageName
    icon获取获取方法:packageManager.getApplicationIcon(applicationInfo)
    应用名称获取方法:packageManager.getApplicationLabel(applicationInfo)

    通过 ResolveInfo 获取具体信息方法:

    包名获取方法:resolve.activityInfo.packageName
    icon获取方法:resolve.loadIcon(packageManager)
    应用名称获取方法:resolve.loadLabel(packageManager).toString()

    获取IMEI的两种方法:

    String imei = ((TelephonyManager) context.getSystemService(TELEPHONY_SERVICE)).getDeviceId();
    String IMEI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMEI)

    相关文章

      网友评论

          本文标题:Android之获取手机信息汇总

          本文链接:https://www.haomeiwen.com/subject/rqwvlktx.html