简体   繁体   中英

Can't get device IMEI

I want to get device IMEI, but It always show the log:

E/GoogleTagManager: Invalid macro: _gtm.loadEventEnabled

I have already added below things

apply plugin: 'com.google.gms.google-services' in build.grade

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> in AndroidManifest.xml.

How can I fixed it? or is there any way to get device IMEI?

Here is my code:

public class MainActivity extends AppCompatActivity {

    public static String deviceIMEI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        deviceIMEI = TelephonyMgr.getDeviceId();
        Log.e("MainActivity", "deviceIMEI: " + deviceIMEI);
    }

}

Here is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="playground.com.pgapp">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@drawable/icon"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:allowBackup="true" >

        <meta-data
            android:name="com.google.android.gms.ads.playground-app-id"
            android:value="ca-app-pub-8130601335284542~7374133691"/>

        <activity android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".CoverActivity"
                  android:screenOrientation="portrait" />

        <activity android:name=".HomePageActivity"
                  android:screenOrientation="portrait" />

        <activity android:name=".PostActivity"
                  android:screenOrientation="portrait"/>

        <activity android:name=".PersonActivity"
                  android:screenOrientation="portrait"/>

        <activity android:name=".FullPostActivity"
            android:screenOrientation="portrait"/>

        <service
            android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
    </application>

</manifest>

first you have to ask to user for access phone permission in you manifest file :-

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

after this you can get IMEI number

You want to call android.telephony.TelephonyManager.getDeviceId() .

This will return whatever string uniquely identifies the device (IMEI on GSM, MEID for CDMA).

You'll need the following permission in your AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

in order to do this.

That being said, be careful about doing this. Not only will users wonder why your application is accessing their telephony stack, it might be difficult to migrate data over if the user gets a new device.

Update: As mentioned in the comments below, this is not a secure way to authenticate users, and raises privacy concerns. It is not recommended. Instead, look at the Google+ Login API if you want to implement a frictionless login system.

The Android Backup API is also available if you just want a lightweight way to persist a bundle of strings for when a user resets their phone (or buys a new device).

Reference this

Use This

use Permission in your mainfest file;

 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

Than use your code

 TelephonyManager  telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    deviceIMEI = telephonyManager .getDeviceId();
    Log.e("MainActivity", "deviceIEMI: " + deviceIMEI);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM