我想要设备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?
这是我的代码:
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>发布于 2019-04-25 07:31:14
首先,您必须向用户询问清单文件中的电话访问权限:-
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>在这之后,你可以得到IMEI号码
发布于 2019-04-25 07:35:58
你想打电话给android.telephony.TelephonyManager.getDeviceId()。
这将返回唯一标识设备的任何字符串( GSM上的IMEI,CDMA的MEID )。
在您的AndroidManifest.xml中需要以下权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />才能做到这一点。
话虽如此,但要小心行事。用户不仅会怀疑您的应用程序为什么要访问他们的电话堆栈,而且如果用户获得了新的设备,可能很难将数据迁移到上面。
更新:如下面的注释所述,这不是对用户进行身份验证的安全方式,也会引发隐私问题。不建议这样做。相反,如果要实现无摩擦登录系统,请查看Google+登录API。
如果您只想要一种轻量级的方法来持久化一捆字符串,当用户重置手机(或购买新设备)时,Android备份API也是可用的。
参考这
发布于 2019-04-25 07:38:19
用这个
在您的主文件中使用许可;
<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);https://stackoverflow.com/questions/55843980
复制相似问题