0

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>
Arjun Vyas
  • 409
  • 5
  • 14
Yunting
  • 25
  • 8
  • are you sure you have permission ? you might need taking permission by asking user lately . – coroutineDispatcher Apr 25 '19 at 07:28
  • @StavroXhardha I had already add android.permission.READ_PHONE_STATE, I add my AndroidMainfest code om my post. – Yunting Apr 25 '19 at 07:35
  • On Which OS version you are testing ? – ADM Apr 25 '19 at 07:36
  • 2
    Why aren't you ever calling `requestPermissions`? Your app won't actually get the permission until you do (specifically until you get the `onRequestPermissionsResult` with `PERMISSION_GRANTED`). Also note that starting with Android Q it is no longer possible for 3rd party apps to read the device's IMEI. – Michael Apr 25 '19 at 07:38
  • @ADM my android is 7.1.1 – Yunting Apr 25 '19 at 07:38
  • 1
    Okay . The problem is what Michael stated above .. You need to request Permission currently you are only checking the status of Permission .To confirm this Run the App on Below API 23 device once. Follow [This](https://stackoverflow.com/questions/33666071/android-marshmallow-request-permission). – ADM Apr 25 '19 at 07:43
  • check my code its working on 7.1.1 – mehul chauhan Apr 25 '19 at 07:50
  • Where is your setContentView(R.layout.activity_main);? – mehul chauhan Apr 25 '19 at 07:55
  • @mehul sorry, when I copy my code, I deleted it, thank you – Yunting Apr 25 '19 at 08:00
  • @ADM ok, I will try it , thank you! – Yunting Apr 25 '19 at 08:01

3 Answers3

0

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

Sandeep Malik
  • 1,972
  • 1
  • 8
  • 17
0

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

0

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);
mehul chauhan
  • 1,792
  • 11
  • 26