Example usage for android.telephony TelephonyManager getDeviceId

List of usage examples for android.telephony TelephonyManager getDeviceId

Introduction

In this page you can find the example usage for android.telephony TelephonyManager getDeviceId.

Prototype

@Deprecated
@SuppressAutoDoc 
@RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
public String getDeviceId() 

Source Link

Document

Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones.

Usage

From source file:com.guardtrax.ui.screens.HomeScreen.java

private void loadPreferenceDataBase() {
    //get the default values
    //List<String> defaults = OptionsScreen.loadDefaults();

    String uniqueID = "";

    //get the IMEI number
    try {/*from ww w.  j ava 2  s  .com*/
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (tm != null)
            uniqueID = tm.getDeviceId();
        if (uniqueID == null || uniqueID.length() == 0) {
            WifiManager wifimanager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            if (wifimanager != null)
                uniqueID = wifimanager.getConnectionInfo().getMacAddress();
        }
        if (uniqueID == null)
            uniqueID = BluetoothAdapter.getDefaultAdapter().getAddress();
        if (uniqueID == null)
            uniqueID = "123456789";
    } catch (Exception e) {
        uniqueID = "987654321";
    }

    String uniqueIdDB = Utility.getUniqueNumber(uniqueID);

    //get the type of phone  (GSM,CDMA) and put into constants
    GTConstants.PHONE_TYPE = getPhoneType();

    //Load into new database 
    preferenceDB.open();

    id = preferenceDB.insertRecord(GTConstants.LOCATIONUPDATESINTERVAL,
            GTConstants.LOCATIONUPDATEDISTANCEINTERVAL, GTConstants.SERVERIP,
            String.valueOf(GTConstants.SERVERPORT), GTConstants.PANIC_NUMBER, uniqueIdDB,
            String.valueOf(GTConstants.ACCELEROMETER_SPEED), GTConstants.LICENSE_ID, GTConstants.PHONE_TYPE,
            GTConstants.REGISTRATION);

    preferenceDB.close();

    //load into constants
    preferenceDB.open();
    cursor = preferenceDB.getRecordByRowID("1");

    saveInConstants(cursor);

    cursor.close();
    preferenceDB.close();

}

From source file:com.codename1.impl.android.AndroidImplementation.java

/**
 * @inheritDoc//from  w  ww . j  a va 2s  . c o  m
 */
public String getProperty(String key, String defaultValue) {
    if (key.equalsIgnoreCase("cn1_push_prefix")) {
        /*if(!checkForPermission(Manifest.permission.READ_PHONE_STATE, "This is required to get notifications")){
        return "";
        }*/
        boolean has = hasAndroidMarket();
        if (has) {
            return "gcm";
        }
        return defaultValue;
    }
    if ("OS".equals(key)) {
        return "Android";
    }
    if ("androidId".equals(key)) {
        return Settings.Secure.getString(getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    if ("cellId".equals(key)) {
        try {
            if (!checkForPermission(Manifest.permission.READ_PHONE_STATE,
                    "This is required to get the cellId")) {
                return defaultValue;
            }
            String serviceName = Context.TELEPHONY_SERVICE;
            TelephonyManager telephonyManager = (TelephonyManager) getContext().getSystemService(serviceName);
            int cellId = ((GsmCellLocation) telephonyManager.getCellLocation()).getCid();
            return "" + cellId;
        } catch (Throwable t) {
            return defaultValue;
        }
    }
    if ("AppName".equals(key)) {

        final PackageManager pm = getContext().getPackageManager();
        ApplicationInfo ai;
        try {
            ai = pm.getApplicationInfo(getContext().getPackageName(), 0);
        } catch (NameNotFoundException e) {
            ai = null;
        }
        String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : null);
        if (applicationName == null) {
            return defaultValue;
        }
        return applicationName;
    }
    if ("AppVersion".equals(key)) {
        try {
            PackageInfo i = getContext().getPackageManager()
                    .getPackageInfo(getContext().getApplicationInfo().packageName, 0);
            return i.versionName;
        } catch (NameNotFoundException ex) {
            ex.printStackTrace();
        }
        return defaultValue;
    }
    if ("Platform".equals(key)) {
        String p = System.getProperty("platform");
        if (p == null) {
            return defaultValue;
        }
        return p;
    }
    if ("User-Agent".equals(key)) {
        String ua = getUserAgent();
        if (ua == null) {
            return defaultValue;
        }
        return ua;
    }
    if ("OSVer".equals(key)) {
        return "" + android.os.Build.VERSION.RELEASE;
    }
    if ("DeviceName".equals(key)) {
        return "" + android.os.Build.MODEL;
    }
    try {
        if ("IMEI".equals(key) || "UDID".equals(key)) {
            if (!checkForPermission(Manifest.permission.READ_PHONE_STATE,
                    "This is required to get the device ID")) {
                return "";
            }
            TelephonyManager tm = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
            String imei = null;
            if (tm != null && tm.getDeviceId() != null) {
                // for phones or 3g tablets
                imei = tm.getDeviceId();
            } else {
                try {
                    imei = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
                } catch (Throwable t) {
                    com.codename1.io.Log.e(t);
                }
            }
            return imei;
        }
        if ("MSISDN".equals(key)) {
            if (!checkForPermission(Manifest.permission.READ_PHONE_STATE,
                    "This is required to get the device ID")) {
                return "";
            }
            TelephonyManager tm = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
            return tm.getLine1Number();
        }
    } catch (Throwable t) {
        // will be caused by no permissions.
        return defaultValue;
    }

    if (getActivity() != null) {
        android.content.Intent intent = getActivity().getIntent();
        if (intent != null) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                String value = extras.getString(key);
                if (value != null) {
                    return value;
                }
            }
        }
    }

    //these keys/values are from the Application Resources (strings values)
    try {
        int id = getContext().getResources().getIdentifier(key, "string",
                getContext().getApplicationInfo().packageName);
        if (id != 0) {
            String val = getContext().getResources().getString(id);
            return val;
        }
    } catch (Exception e) {
    }
    return System.getProperty(key, super.getProperty(key, defaultValue));
}