首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法获得设备IMEI

无法获得设备IMEI
EN

Stack Overflow用户
提问于 2019-04-25 07:27:02
回答 3查看 439关注 0票数 0

我想要设备IMEI,但它总是显示日志:

E/GoogleTagManager:无效宏:_gtm.loadEventEnabled

我已经添加了以下内容

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

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

我怎么能修好它?或者有什么方法可以得到设备IMEI?

这是我的代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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>
EN

回答 3

Stack Overflow用户

发布于 2019-04-25 07:31:14

首先,您必须向用户询问清单文件中的电话访问权限:-

代码语言:javascript
复制
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

在这之后,你可以得到IMEI号码

票数 0
EN

Stack Overflow用户

发布于 2019-04-25 07:35:58

你想打电话给android.telephony.TelephonyManager.getDeviceId()

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

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

代码语言:javascript
复制
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

才能做到这一点。

话虽如此,但要小心行事。用户不仅会怀疑您的应用程序为什么要访问他们的电话堆栈,而且如果用户获得了新的设备,可能很难将数据迁移到上面。

更新:如下面的注释所述,这不是对用户进行身份验证的安全方式,也会引发隐私问题。不建议这样做。相反,如果要实现无摩擦登录系统,请查看Google+登录API

如果您只想要一种轻量级的方法来持久化一捆字符串,当用户重置手机(或购买新设备)时,Android备份API也是可用的。

参考

票数 0
EN

Stack Overflow用户

发布于 2019-04-25 07:38:19

用这个

在您的主文件中使用许可;

代码语言:javascript
复制
 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

而不是使用你的代码

代码语言:javascript
复制
 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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55843980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档