gpt4 book ai didi

android - 应用程序在获取设备 ID 时崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:15 24 4
gpt4 key购买 nike

我正在使用以下代码获取设备 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)

应用特别在下一行崩溃。

tmDevice = "" + tm.getDeviceId();

它提示权限问题,但它已经被添加并且在其他设备上也能正常工作。

我的 list :

<?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>

任何帮助都会很棒。

最佳答案

由于 READ_PHONE_STATE 在 Android M (6.0) 中被认为是一个危险的权限,您需要在运行时请求该权限,然后在您的 Activity 中尝试获取 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 权限的更多信息,请查看 this

祝你好运,编码愉快!

关于android - 应用程序在获取设备 ID 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35897361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com