简体   繁体   English

无法获取设备IMEI

[英]Can't get device IMEI

I want to get device IMEI, but It always show the log: 我想获取设备IMEI,但它总是显示日志:

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

I have already added below things 我已经添加了以下内容

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

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

How can I fixed it? 我该如何修理它? or is there any way to get device IMEI? 或者有没有办法让设备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: 这是我的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 在此之后,您可以获得IMEI号码

You want to call android.telephony.TelephonyManager.getDeviceId() . 你想调用android.telephony.TelephonyManager.getDeviceId()

This will return whatever string uniquely identifies the device (IMEI on GSM, MEID for CDMA). 这将返回唯一标识设备的字符串(GSM上的IMEI,CDMA的MEID)。

You'll need the following permission in your AndroidManifest.xml: 您需要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. 相反,如果您要实施无摩擦登录系统,请查看Google+登录API

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). 如果您只想在用户重置手机(或购买新设备)时使用轻量级方法来保留一串字符串,那么Android备份API也可用。

Reference this 参考这个

Use This 用这个

use Permission in your mainfest file; 在mainfest文件中使用Permission;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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