应用程序崩溃获取设备ID

时间:2016-03-09 16:35:52

标签: android

我正在使用以下代码来获取设备ID。

public static String getDeviceUuId(Activity context) {
        String UUID = "";
        String android_id = Secure.getString(context.getApplicationContext().getContentResolver(), Secure.ANDROID_ID);

        final TelephonyManager tm = (TelephonyManager) context.getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);

        if (tm != null) {
            final String tmDevice, tmSerial, androidId;
            tmDevice = "" + tm.getDeviceId();
            tmSerial = "" + tm.getSimSerialNumber();
            androidId = "" + Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);

            UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
            UUID = deviceUuid.toString();
            return UUID;
        }
        return UUID;
    }

效果很好,但对于nexus 5(OS 6.0.1),我遇到了崩溃。以下是我的崩溃日志。

3-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: FATAL EXCEPTION: main
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: Process: com.my.package, PID: 13825
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime: java.lang.SecurityException: getDeviceId: Neither user 10159 nor current process has android.permission.READ_PHONE_STATE.
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1620)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1573)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4207)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:706)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at com.my.package.utils.DeveloperHelper.getDeviceUuId(DeveloperHelper.java:26)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at com.my.package.model.request.Device.<init>(Device.java:22)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at com.my.package.model.request.LoginObject.<init>(LoginObject.java:23)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at com.my.package.activities.LoginActivity.onClick(LoginActivity.java:57)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at android.view.View.performClick(View.java:5204)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21153)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-09 20:53:12.442 13825-13825/com.my.package E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

App专门在以下行崩溃。

tmDevice = "" + tm.getDeviceId();

它抱怨许可,但它已经在其他设备上添加并正常工作。

我的宣言:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.package"
    android:versionCode="2"
    android:versionName="2.0">

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="22" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <uses-feature
        android:name="android.hardware.camera"
        android:required="true" />
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:name=".BaseApplication"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.my.package.activities.SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan|stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.my.package.activities.StarterActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan|stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.my.package.activities.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan|stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <!-- FACEBOOK -->
        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />
        <meta-data
            android:name="com.facebook.sdk.ApplicationName"
            android:value="@string/app_name" />

        <!-- FACEBOOK -->


        <!-- PLAY SERVICES -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

任何帮助都会很棒。

3 个答案:

答案 0 :(得分:8)

由于READ_PHONE_STATE在Android M(6.0)中被视为危险权限,因此您需要在运行时请求权限,然后在尝试获取活动中的UUID之前执行以下操作:

宣布

 private static final int REQUEST_PHONE_STATE = 1;

然后在尝试获取UUID之前,您可以调用此方法:

private void checkForPhoneStatePermission(){

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

         if (ContextCompat.checkSelfPermission(MainActivity.this,
                    Manifest.permission.READ_PHONE_STATE)
                    != PackageManager.PERMISSION_GRANTED) {

                // Should we show an explanation?
                if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                        Manifest.permission.READ_PHONE_STATE)) {

                    // Show an explanation to the user *asynchronously* -- don't block
                    // this thread waiting for the user's response! After the user
                    // sees the explanation, try again to request the permission.

                    showPermissionMessage();

                } else {

                    // No explanation needed, we can request the permission.
                    ActivityCompat.requestPermissions(MainActivity.this,
                            new String[]{Manifest.permission.READ_PHONE_STATE},
                            REQUEST_PHONE_STATE);
                }
            }
        }else{
          //... Permission has already been granted, obtain the UUID
          getDeviceUuId(MainActivity.this);
        }

    }else{
        //... No need to request permission, obtain the UUID
        getDeviceUuId(MainActivity.this);
    }
}


private void showPermissionMessage(){
 new AlertDialog.Builder(this)
                .setTitle("Read phone state")
                .setMessage("This app requires the permission to read phone state to continue")
                .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.READ_PHONE_STATE},
                        REQUEST_PHONE_STATE);
            }
        }).create().show();
}

然后将其添加到MainActivity类,该类处理权限请求的结果:

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch(requestCode){
            case REQUEST_PHONE_STATE:

                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

                   // .. Can now obtain the UUID
                   getDeviceUuId(MainActivity.this);
                }else{
                    Toast.makeText(MainActivity.this, "Unable to continue without granting permission", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

有关Android M Permission的更多信息,请查看this

祝你好运,快乐的编码!

答案 1 :(得分:3)

答案是运行时权限

  

从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时。

请阅读文档并查找详细信息here

答案 2 :(得分:1)

marshmallow中,Android采用permissions行事的新方式,实际上您必须询问用户是否要授予对特定permission的访问权限。这就是为什么您的代码在低于6.0的版本上正常工作但不在6.0上工作的原因 看看这个link,了解如何在棉花糖中做到这一点