Categories: Android Guides

Android how to get important information programatically

How to get Android App Version Name and Code

private void getAppVersionNameAndCode(){
        PackageManager pManager = getPackageManager();
        try {
            PackageInfo pInfo = pManager.getPackageInfo(getPackageName(), 0);
            mAppVersionName = pInfo.versionName;

            if (Build.VERSION.SDK_INT >= 28) {
                mAppVersionCode = (int) PackageInfoCompat.getLongVersionCode(pInfo);
            }else {
                mAppVersionCode = pInfo.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

How to get App package name

    private String getAppPackageName(Context ctx){
        return ctx.getPackageName();
    }

How to get Android OS Version Name and Code

    private String getAndroidOSVersion(){
        return Build.VERSION.CODENAME;
    }
    private int getAndroidOSVersionCode(){
        return Build.VERSION.SDK_INT;
    }

How to get device model name and manufacturer

    private String getDeviceModelName(){
        return android.os.Build.MODEL;
    }

    private String getDeviceManufacturer(){
        return Build.MANUFACTURER;
    }

How to get Country Code

    private String getCountryCodeByLocale(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            return getResources().getConfiguration().getLocales().get(0).getCountry();
        else
            return getResources().getConfiguration().locale.getCountry();

    }

    private String getCountryCodeByTelephony(){
        String countryCode = "";
        try {
            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            if(telephonyManager!=null) {
                countryCode = telephonyManager.getNetworkCountryIso();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return countryCode;
    }

How to get IMEI

Add READ_PHONE_STATE permission in AndroidManifest.xml

    private static final int PERMISSION_REQUEST_CODE = 123;
    private String getImeiCode() {
        String imeiCode = "Unknown";
        try {
            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_REQUEST_CODE);
                }
                else imeiCode = telephonyManager.getImei();

            }else{
                try {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_REQUEST_CODE);
                        }
                        else imeiCode = telephonyManager.getDeviceId();

                    }else {
                        imeiCode = telephonyManager.getDeviceId();
                    }
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return imeiCode;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PERMISSION_REQUEST_CODE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            getImeiCode();
        }
    }

How to get screen height and width

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int height = displayMetrics.heightPixels;
        int width = displayMetrics.widthPixels;

How to get Android Advertising ID

Add play services ads dependency in app’s gradle file.

    implementation 'com.google.android.gms:play-services-ads:17.1.3'

Function to get advertising id. Since AdvertisingIdClient.getAdvertisingIdInfo cannot be called on the main UI thread, we need to create a background thread to call the function.

    public void getAdvertisingId(Context context) {
        final WeakReference<Context> ctx = new WeakReference<>(context);
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(ctx.get());
                    String adId = adInfo != null ? adInfo.getId() : null;
                    // Use advertising id

                } catch (IOException | GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException exception) {
                    // Error handling if needed
                }
            }
        });
    }

Share

Recent Posts

  • Android

How to use ViewModel in Android

Android provides multiple methods to execute long running code off the UI thread while posting the results of the code…

2 months ago
  • Android
  • Guides

How to use DialogFragments instead of AlertDialogs in Android

In this post we will be discussing the use of DialogFragments in Android. We will see how to migrate away…

2 months ago
  • Firebase

Save and Access Environment Variables in Firebase

Environment variables in the cloud functions allows you to set/get key values at the time of deployment which can later…

4 months ago
  • Android

How to migrate to AndroidX support library

AndroidX is the latest Android support library package and is an improved version of older support libraries. Android Studio seamlessly…

5 months ago
  • Ads Integration

Unity: How to automatically add Admob App ID

Admob is one of the most used Ad network used by developers all over the world. In recent sdk update…

8 months ago
  • Android
  • Guides

How to use Accessibility Service in Android

Accessibility Service is used to assist users with disabilities. Accessibility services are background services which is invoked by the system…

8 months ago