Detailed Example of Android Access to Mobile Phone and APP Information

Keywords: Mobile Java Android Mac

The frost wind is getting tighter and colder. Listen to the voice of lone geese and send out a sound of sadness. This poem in "Royal Street Journey" is just the right one to describe Beijing in this season. It's time to test perseverance when the frost blows and running becomes more and more reluctant to take off your coat.

Back to the point, when developing APP for Android mobile phone, it is often necessary to obtain relevant information about mobile phone, such as system version number, screen resolution and MAC. Today let's talk about how to get information about mobile phones.

First of all, we can create a new class, which needs a context member variable. For the others, we can code directly without verbosity.

Get screen resolution:

//Acquisition resolution
        String resolution = "";
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        resolution = screenWidth + "*" + screenHeight;

Access operators:

//Acquisition Operator
        String providersName = "";
        TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        String IMSI = telephonyManager.getSubscriberId();
        if (IMSI != null) {
            if (IMSI.startsWith("46000") || IMSI.startsWith("46002") || IMSI.startsWith("46007")) {
                providersName = "China Mobile";
            } else if (IMSI.startsWith("46001") || IMSI.startsWith("46006")) {
                providersName = "China Unicom";
            } else if (IMSI.startsWith("46003")) {
                providersName = "China Telecom";
            } else {
                providersName = "Other";
            }
        } else {
            providersName = "Unable to access operator information";
        }

Access to Internet:

/Access to networking
        String access = "";
        ConnectivityManager connectionManager = (ConnectivityManager) mContext.getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo netWorkInfo = connectionManager.getActiveNetworkInfo();
        access = netWorkInfo.getTypeName();

Get the operating system version:

/Getting Operating System Version
        String osVersion = "";
        osVersion = android.os.Build.VERSION.RELEASE;

Get the APP version:

//Get the APP version
        try {
            PackageManager pm = mContext.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
            if (pi != null) {
                errorPram.setAppVersion(pi.versionName == null ? "" : pi.versionName);
            }
        } catch (NameNotFoundException e) {

        }

Get device ID:

//Get device ID
        String deviceId = "";
        deviceId = telephonyManager.getDeviceId();

Get MAC:

//Get MAC
        String macAddress = "";
        WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = (null == wifiManager ? null : wifiManager.getConnectionInfo());
        if (null != info) {
            macAddress = info.getMacAddress();
        }

Get the mobile phone model:

//Get the Model of Mobile Phone
        String model = "";
        model = android.os.Build.MODEL;

Get IMEI:

//Get IMEI
        String imei = "";
        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Activity.TELEPHONY_SERVICE);
        if (tm != null) {
            imei = tm.getDeviceId();
        }

Get error reports:

//Getting error reports
        String report = "";
        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        ex.printStackTrace(printWriter);
        Throwable cause = ex.getCause();
        while (cause != null) {
            cause.printStackTrace(printWriter);
            cause = cause.getCause();
        }
        printWriter.close();
        report = writer.toString();

Note that if you want to get an error report, this class needs to inherit the Thread.UncaughtExceptionHandler interface to get an instance of the exception that is generated, which is the ex in the fourth line of the code.

Using mobile phones for testing, we can get the following information:

10-27 16:50:31.404 31481-31481/com.radi.rescue E/66666: Resolution: 1080*1792
10-27 16:50:31.408 31481-31481/com.radi.rescue E/66666: Operator: China Unicom
10-27 16:50:31.410 31481-31481/com.radi.rescue E/66666: Networking mode: WIFI
10-27 16:50:31.410 31481-31481/com.radi.rescue E/66666: Operating System Version: 8.0.0
10-27 16:50:31.421 31481-31481/com.radi.rescue E/66666: app Version: 2.6.5
10-27 16:50:31.421 31481-31481/com.radi.rescue E/66666: equipment ID: 863127037355473
10-27 16:50:31.437 31481-31481/com.radi.rescue E/66666: MAC: b0:89:00:d7:36:e8
10-27 16:50:31.437 31481-31481/com.radi.rescue E/66666: Mobile phone model: FRD-AL10
10-27 16:50:31.438 31481-31481/com.radi.rescue E/66666: IMEI: 863127037355473
10-27 16:50:31.445 31481-31481/com.radi.rescue E/66666: Error reporting: java.lang.RuntimeException: Unable to start activity ComponentInfozuodakuohaocom.radi.rescue/com.radi.rescue.info.AddSiteActivityyoudakuohao: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
                                                            at android.app.ActivityThread.-wrap12(Unknown Source:0)
                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
                                                            at android.os.Handler.dispatchMessage(Handler.java:108)
                                                            at android.os.Looper.loop(Looper.java:166)
                                                            at android.app.ActivityThread.main(ActivityThread.java:7425)
                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
                                                            at com.radi.rescue.info.AddSiteActivity.onCreate(AddSiteActivity.java:87)
                                                            at android.app.Activity.performCreate(Activity.java:7372)
                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302) 
                                                            at android.app.ActivityThread.-wrap12(Unknown Source:0) 
                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891) 
                                                            at android.os.Handler.dispatchMessage(Handler.java:108) 
                                                            at android.os.Looper.loop(Looper.java:166) 
                                                            at android.app.ActivityThread.main(ActivityThread.java:7425) 
                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 
                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 
                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
                                                            at com.radi.rescue.info.AddSiteActivity.onCreate(AddSiteActivity.java:87)
                                                            at android.app.Activity.performCreate(Activity.java:7372)
                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
                                                            at android.app.ActivityThread.-wrap12(Unknown Source:0)
                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
                                                            at android.os.Handler.dispatchMessage(Handler.java:108)
                                                            at android.os.Looper.loop(Looper.java:166)
                                                            at android.app.ActivityThread.main(ActivityThread.java:7425)
                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

This article has so much to share, and you are welcome to add and comment.

Posted by Cogen2 on Wed, 23 Jan 2019 09:03:15 -0800