get a unique id for a device, need permission android.permission.READ_PHONE_STATE. - Android Hardware

Android examples for Hardware:Device ID

Description

get a unique id for a device, need permission android.permission.READ_PHONE_STATE.

Demo Code


//package com.java2s;
import java.lang.reflect.Method;
import android.content.Context;
import android.telephony.TelephonyManager;

public class Main {
    /**/*from w w  w  .ja v a2 s .c o m*/
     * get a unique id for a device, need permission android.permission.READ_PHONE_STATE.
     * @param context
     * @return a unique id
     */
    public static String getUniqueId(Context context) {
        String imei = getIMEI(context);
        String androidId = getAndroidId(context);
        String sn = getSerialNumber();
        String sim = getSimSerialNumber(context);
        String line1 = getPhoneNumber(context);
        String uniqueId = imei + androidId + sn + sim + line1;
        return uniqueId;
    }

    public static String getIMEI(Context context) {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getDeviceId();
    }

    public static String getAndroidId(Context context) {
        return android.provider.Settings.Secure.getString(
                context.getContentResolver(),
                android.provider.Settings.Secure.ANDROID_ID);
    }

    /**
     * getSerialNumber
     *
     * @return result is same to getSerialNumber1()
     */
    public static String getSerialNumber() {
        String serial = null;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            serial = (String) get.invoke(c, "ro.serialno");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return serial;
    }

    public static String getSimSerialNumber(Context context) {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getSimSerialNumber();
    }

    public static String getPhoneNumber(Context context) {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        String line1 = tm.getLine1Number();
        if (line1 == null || line1.trim().isEmpty()) {
            line1 = "null";
        }
        return line1;
    }
}

Related Tutorials