Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

import android.os.Build;

import android.provider.Settings;
import android.telephony.TelephonyManager;

public class Main {
    /**
     * Unique ID of the Android device
     */
    public static String ANDROID_ID;
    /**
     * Serial number of the SIM, null if unavailable
     */
    public static String SIM_SERIAL_NUMBER;

    /**
     * Reads the phone state in order to get SIM serial number and AndroidID, those values are saved in static
     * attributes in this class
     *
     * @param context
     */
    public static void readPhoneState(Context context) {

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        //Get SIM serial number
        SIM_SERIAL_NUMBER = telephonyManager.getSimSerialNumber();
        if (SIM_SERIAL_NUMBER == null) {
            SIM_SERIAL_NUMBER = "";
        }

        /*
         * Settings.Secure.ANDROID_ID returns the unique DeviceID
         * Works for Android 2.2 and above
         */
        ANDROID_ID = getUniqueDeviceId(context);
    }

    /**
     * Gets an unique device Id depending on the sdk version
     *
     * @param context
     * @return
     */
    public static String getUniqueDeviceId(Context context) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
            return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } else {
            final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            return tm.getDeviceId();
        }
    }
}