android獲取設備和應用信息

jopen 9年前發布 | 2K 次閱讀 Java Android

創建一個android應用(AndroidTest),所需權限如下(AndroidManifest.xml文件):

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  

接下來是MainActivity.java文件:

    package com.example.androidtest;

import org.json.JSONException;  
import org.json.JSONObject;  

import android.net.wifi.WifiInfo;  
import android.net.wifi.WifiManager;  
import android.os.Bundle;  
import android.provider.Settings.Secure;  
import android.app.Activity;  
import android.content.Context;  
import android.content.pm.ApplicationInfo;  
import android.content.pm.PackageManager;  
import android.content.pm.PackageManager.NameNotFoundException;  
import android.telephony.TelephonyManager;  
import android.telephony.cdma.CdmaCellLocation;  
import android.telephony.gsm.GsmCellLocation;  
import android.view.Menu;  

public class MainActivity extends Activity {  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        getInfo();  
    }  

    public void getInfo() {  
        try {  
            StringBuilder strLog = new StringBuilder();  
            Context ctx = this.getApplicationContext();  

            /** 
             * 1.獲取應用信息 
             *  
             * 要想獲取更多應用相關信息請查閱PackageManager、ApplicationInfo資料 
             */  

            // 獲取應用名稱  
            String appName = getAppName(ctx);  
            strLog.append("應用名稱:" + appName + "\r\n");  

            // 獲取應用包名稱  
            String packName = getPackName(ctx);  
            strLog.append("應用包名稱:" + packName + "\r\n");  

            // 獲取應用版本  
            String verName = getVerName(ctx, packName);  
            strLog.append("應用版本名稱:" + verName + "\r\n");  

            // 獲取應用版本號  
            int verCode = getVerCode(ctx, packName);  
            strLog.append("應用版本號:" + verCode + "\r\n");  

            /** 
             * 2.獲取設備信息 
             */  
            // 獲取手機型號  
            String model = getPhoneModel();  
            strLog.append("手機型號:" + model + "\r\n");  

            // 獲取手機號碼  
            String phoneNum = getLineNum(ctx);  
            strLog.append("手機號碼:" + phoneNum + "\r\n");  

            // 獲取移動用戶標志,IMSI  
            String imsi = getSubscriberId(ctx);  
            strLog.append("IMSI:" + imsi + "\r\n");  

            // 獲取設備ID  
            String devID = getDeviceID(ctx);  
            strLog.append("設備ID:" + devID + "\r\n");  

            // 獲取SIM卡號  
            String sim = getSim(ctx);  
            strLog.append("SIM卡號:" + sim + "\r\n");  

            // 獲取基站信息  
            SCell cellInfo = getCellInfo(ctx);  
            String strCell = "";  
            if (cellInfo != null) {  
                strCell = cellInfo.toJSON().toString();  
            }  
            strLog.append("基站信息:" + strCell + "\r\n");  

            // 獲取Mac地址  
            String mac = getMac(ctx);  
            strLog.append("Mac地址:" + mac + "\r\n");  

            System.out.println(strLog.toString());  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  

    /** 
     * 獲取應用包名稱 
     */  
    public String getPackName(Context ctx) {  
        return ctx.getPackageName();  
    }  

    /** 
     * 獲取應用版本名稱 
     */  
    public String getVerName(Context ctx, String packName) {  
        String verName = "";  
        try {  
            verName = ctx.getPackageManager().getPackageInfo(packName, 0).versionName;  
        } catch (NameNotFoundException e) {  
        }  
        return verName;  
    }  

    /** 
     * 獲取應用版本號 
     */  
    public int getVerCode(Context context, String packName) {  
        int versionCode = 0;  
        try {  
            versionCode = context.getPackageManager().getPackageInfo(packName,  
                    0).versionCode;  
        } catch (NameNotFoundException e) {  
        }  
        return versionCode;  
    }  

    /** 
     * 獲取應用名稱 
     */  
    public String getAppName(Context ctx) {  
        String appName = "";  
        try {  
            PackageManager packManager = ctx.getPackageManager();  
            ApplicationInfo appInfo = ctx.getApplicationInfo();  
            appName = (String) packManager.getApplicationLabel(appInfo);  
        } catch (Exception e) {  
        }  
        return appName;  
    }  

    /** 
     * 獲取手機型號 
     *  
     * android.os.Build提供以下信息: 
     *      String  BOARD   The name of the underlying board, like "goldfish".  
     *      String  BRAND   The brand (e.g., carrier) the software is customized for, if any.    
     *      String  DEVICE  The name of the industrial design.   
     *      String  FINGERPRINT     A string that uniquely identifies this build.    
     *      String  HOST          
     *      String  ID  Either a changelist number, or a label like "M4-rc20".   
     *      String  MODEL   The end-user-visible name for the end product.   
     *      String  PRODUCT     The name of the overall product.          
     *      String  TAGS    Comma-separated tags describing the build, like "unsigned,debug".        
     *      long    TIME              
     *      String  TYPE    The type of build, like "user" or "eng".         
     *      String  USER      
     */  
    public String getPhoneModel() {  
        return android.os.Build.MODEL;  
    }  

    /** 
     * 獲取手機號碼,一般獲取不到 
     *  
     * 用到的權限:  
     *  <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
     *  
     * 要想獲取更多電話、數據、移動網絡相關信息請查閱TelephonyManager資料 
     */  
    public String getLineNum(Context ctx) {  
        String strResult = "";  
        TelephonyManager telephonyManager = (TelephonyManager) ctx  
                .getSystemService(Context.TELEPHONY_SERVICE);  
        if (telephonyManager != null) {  
            strResult = telephonyManager.getLine1Number();  
        }  
        return strResult;  
    }  

    /** 
     * 獲取移動用戶標志,IMSI 
     *  
     * 用到的權限: 
     *  <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
     */  
    public String getSubscriberId(Context ctx) {  
        String strResult = "";  
        TelephonyManager telephonyManager = (TelephonyManager) ctx  
                .getSystemService(Context.TELEPHONY_SERVICE);  
        if (telephonyManager != null) {  
            strResult = telephonyManager.getSubscriberId();  
        }  
        return strResult;  
    }  

    /** 
     * 獲取設備ID 
     *  
     * 用到的權限: 
     *  <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
     */  
    public String getDeviceID(Context ctx) {  
        String strResult = null;  
        TelephonyManager telephonyManager = (TelephonyManager) ctx  
                .getSystemService(Context.TELEPHONY_SERVICE);  
        if (telephonyManager != null) {  
            strResult = telephonyManager.getDeviceId();  
        }  
        if (strResult == null) {  
            strResult = Secure.getString(ctx.getContentResolver(),  
                    Secure.ANDROID_ID);  
        }  
        return strResult;  
    }  

    /** 
     * 獲取SIM卡號 
     *  
     * 用到的權限: 
     *  <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
     */  
    public String getSim(Context ctx) {  
        String strResult = "";  
        TelephonyManager telephonyManager = (TelephonyManager) ctx  
                .getSystemService(Context.TELEPHONY_SERVICE);  
        if (telephonyManager != null) {  
            strResult = telephonyManager.getSimSerialNumber();  
        }  
        return strResult;  
    }  

    /** 
     * 獲取Wifi Mac地址 
     *  
     * 要想獲取更多Wifi相關信息請查閱WifiInfo資料 
     *  
     * 用到的權限: 
     *  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
     */  
    public String getMac(Context ctx) {  

        WifiManager wifiManager = (WifiManager) ctx  
                .getSystemService(Context.WIFI_SERVICE);  
        if (wifiManager != null) {  
            WifiInfo wi = wifiManager.getConnectionInfo();  
            return wi.getMacAddress();  
        }  
        return null;  
    }  

    /** 
     * 獲取基站信息 
     *  
     * 用到的權限:  
     * <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
     * <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
     */  
    public SCell getCellInfo(Context ctx) {  
        SCell cell = new SCell();  
        TelephonyManager tm = null;  
        try {  
            tm = (TelephonyManager) ctx  
                    .getSystemService(Context.TELEPHONY_SERVICE);  
        } catch (Exception e) {  
            return null;  
        }  
        // IMSI號前面3位460是國家,緊接著后面2位00 02是中國移動,01是中國聯通,03是中國電信。  
        String IMSI = tm.getSubscriberId();  

        if (IMSI != null) {  
            if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {  
                cell.NETWORK_TYPE = "CHINA MOBILE";  

                GsmCellLocation location = (GsmCellLocation) tm  
                        .getCellLocation();  
                if (location == null) {  
                    cell = null;  
                } else {  
                    String operator = tm.getNetworkOperator();  
                    if (operator.length() > 4) {  
                        int mcc = Integer.parseInt(operator.substring(0, 3));  
                        int mnc = Integer.parseInt(operator.substring(3));  
                        int cid = location.getCid();  
                        int lac = location.getLac();  

                        cell.MCC = mcc;  
                        cell.MNC = mnc;  
                        cell.LAC = lac;  
                        cell.CID = cid;  
                    } else {  
                        cell = null;  
                    }  
                }  
            } else if (IMSI.startsWith("46001")) {  
                cell.NETWORK_TYPE = "CHINA UNICOM";  

                GsmCellLocation location = (GsmCellLocation) tm  
                        .getCellLocation();  
                if (location == null) {  
                    cell = null;  
                } else {  
                    String operator = tm.getNetworkOperator();  
                    if (operator.length() > 4) {  
                        int mcc = Integer.parseInt(operator.substring(0, 3));  
                        int mnc = Integer.parseInt(operator.substring(3));  
                        int cid = location.getCid();  
                        int lac = location.getLac();  

                        cell.MCC = mcc;  
                        cell.MNC = mnc;  
                        cell.LAC = lac;  
                        cell.CID = cid;  
                    } else {  
                        cell = null;  
                    }  
                }  
            } else if (IMSI.startsWith("46003")) {  
                cell.NETWORK_TYPE = "CHINA TELECOM";  

                CdmaCellLocation location = (CdmaCellLocation) tm  
                        .getCellLocation();  
                if (location == null) {  
                    cell = null;  
                } else {  
                    String operator = tm.getNetworkOperator();  
                    if (operator.length() > 4) {  
                        int mcc = Integer.parseInt(operator.substring(0, 3));  
                        int mnc = Integer.parseInt(operator.substring(3));  
                        int cid = location.getBaseStationId();  
                        int lac = location.getNetworkId();  

                        cell.MCC = mcc;  
                        cell.MNC = mnc;  
                        cell.LAC = lac;  
                        cell.CID = cid;  
                    } else {  
                        cell = null;  
                    }  
                }  
            } else {  
                // cell.NETWORK_TYPE = "UNDENTIFIED";  
                cell = null;  
            }  
        } else {  
            cell = null;  
        }  
        return cell;  
    }  

    /** 
     * 基站信息 
     */  
    class SCell {  

        public String NETWORK_TYPE;  

        public int MCC;  

        public int MNC;  

        public int LAC;  

        public int CID;  

        public JSONObject toJSON() throws JSONException {  
            JSONObject json = new JSONObject();  
            json.put("network_type", NETWORK_TYPE);  
            json.put("mcc", MCC);  
            json.put("MNC", MNC);  
            json.put("LAC", LAC);  
            json.put("CID", CID);  
            return json;  
        }  
    }  

    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.activity_main, menu);  
        return true;  
    }  

}  </pre> 


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!