获取手机设备ID

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Ansionnal/article/details/8231159

--------------------获取手机设备ID

String imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getDeviceId();

发现Android Pad没有IMEI

用此方法获取设备id:

private String getDeviceId() {
        String android_id = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
        return android_id;
    }

展开阅读全文

获取设备ID报错

04-16

package com.hw.shebei;rnrnimport android.app.Activity;rnimport android.content.Context;rnimport android.os.Bundle;rnimport android.telephony.TelephonyManager;rnrnpublic class ShebeiActivity extends Activity rn /** Called when the activity is first created. */rn private Context context;rn @Overridern public void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.main);rn rn context = ShebeiActivity.this;rn System.out.println(getSerialno(context));rn rn rn public static String getSerialno(Context context) rn TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);rn return tm.getDeviceId();rn rnrnrnrn错误信息:rnrn04-16 08:19:06.697: W/dalvikvm(345): threadid=1: thread exiting with uncaught exception (group=0x4001d800)rn04-16 08:19:06.731: E/AndroidRuntime(345): FATAL EXCEPTION: mainrn04-16 08:19:06.731: E/AndroidRuntime(345): java.lang.RuntimeException: Unable to start activity ComponentInfocom.ij.shebei/com.ij.shebei.ShebeiActivity: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10038 nor current process has android.permission.READ_PHONE_STATE.rn04-16 08:19:06.731: E/AndroidRuntime(345): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)rn04-16 08:19:06.731: E/AndroidRuntime(345): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)rn04-16 08:19:06.731: E/AndroidRuntime(345): at android.app.ActivityThread.access$2300(ActivityThread.java:125)rn04-16 08:19:06.731: E/AndroidRuntime(345): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)rn04-16 08:19:06.731: E/AndroidRuntime(345): at android.os.Handler.dispatchMessage(Handler.java:99)rn04-16 08:19:06.731: E/AndroidRuntime(345): at android.os.Looper.loop(Looper.java:123)rn04-16 08:19:06.731: E/AndroidRuntime(345): at android.app.ActivityThread.main(ActivityThread.java:4627)rn04-16 08:19:06.731: E/AndroidRuntime(345): at java.lang.reflect.Method.invokeNative(Native Method)rn04-16 08:19:06.731: E/AndroidRuntime(345): at java.lang.reflect.Method.invoke(Method.java:521)rn04-16 08:19:06.731: E/AndroidRuntime(345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)rn04-16 08:19:06.731: E/AndroidRuntime(345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)rn04-16 08:19:06.731: E/AndroidRuntime(345): at dalvik.system.NativeStart.main(Native Method)rn04-16 08:19:06.731: E/AndroidRuntime(345): Caused by: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10038 nor current process has android.permission.READ_PHONE_STATE.rn04-16 08:19:06.731: E/AndroidRuntime(345): at android.os.Parcel.readException(Parcel.java:1247)rn04-16 08:19:06.731: E/AndroidRuntime(345): at android.os.Parcel.readException(Parcel.java:1235)rn04-16 08:19:06.731: E/AndroidRuntime(345): at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getDeviceId(IPhoneSubInfo.java:142)rn04-16 08:19:06.731: E/AndroidRuntime(345): at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:187)rn04-16 08:19:06.731: E/AndroidRuntime(345): at com.hw.shebei.ShebeiActivity.getSerialno(ShebeiActivity.java:22)rn04-16 08:19:06.731: E/AndroidRuntime(345): at com.hw.shebei.ShebeiActivity.onCreate(ShebeiActivity.java:17)rn04-16 08:19:06.731: E/AndroidRuntime(345): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)rn04-16 08:19:06.731: E/AndroidRuntime(345): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)rn04-16 08:19:06.731: E/AndroidRuntime(345): ... 11 morern 论坛

没有更多推荐了,返回首页