无法获取设备IMEI

时间:2019-04-25 07:27:03

标签: java android google-tag-manager

我想获取设备IMEI,但它始终显示日志:

  

E / GoogleTagManager:无效的宏:_gtm.loadEventEnabled

我已经添加了以下内容

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

<uses-permission android:name="android.permission.READ_PHONE_STATE" />在AndroidManifest.xml中。

我该如何解决?还是有什么方法可以获取设备IMEI?

这是我的代码:

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);
    }

}

这是我的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>

3 个答案:

答案 0 :(得分:0)

首先,您必须在清单文件中要求用户访问电话权限:-

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

在此之后,您可以获得IMEI号码

答案 1 :(得分:0)

您要致电android.telephony.TelephonyManager.getDeviceId()

这将返回唯一标识设备的任何字符串(GSM上的IMEI,CDMA上的MEID)。

您在AndroidManifest.xml中需要以下权限:

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

为此。

话虽这么说,但要小心。用户不仅会想知道为什么您的应用程序正在访问他们的电话堆栈,而且如果用户购买了新设备,可能很难迁移数据。

更新:如下面的评论所述,这不是验证用户身份的安全方法,并且会引起隐私问题。不推荐。相反,如果您想实现无摩擦的登录系统,请查看Google+ Login API

如果您只是想以一种轻巧的方式在用户重置手机(或购买新设备)时保留一串字符串,那么Android Backup API也可以使用。

引用this

答案 2 :(得分:0)

使用此

在mainfest文件中使用权限;

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

比使用您的代码

 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);