我需要使用Secure.getString()获取设备ID

我正在使用上下文

 public static String getDeviceId(Context context) {
        String currentDeviceId = SharedPreferencesUtil.getCurrentDeviceId();
        if (currentDeviceId == null) {
            context = Secure.getString(context.getContentResolver(), "android_id");
            if (context == null || context.trim().isEmpty() || "9774d56d682e549c".equalsIgnoreCase(context)) {
                context = Build.SERIAL;
                if (context == null || context.trim().isEmpty() != null) {
                    context = UUID.randomUUID().toString();
                } else {
                    context = Build.SERIAL;
                }
            }
            if ("unknown".equals(context)) {
                context = Build.SERIAL;
                if (context == null || context.trim().isEmpty() != null) {
                    context = UUID.randomUUID().toString();
                } else {
                    context = Build.SERIAL;
                }
            }
            secondDeviceId = context;



此行显示以下错误

context = Secure.getString(context.getContentResolver(), "android_id");



不兼容的类型。
需要:
android.content.Context
发现:
java.lang.String

最佳答案

根据@vlumi注释和调试,我发现以下代码可以解决此问题。
现在已修复,只需要用字符串替换上下文即可。

public static String getDeviceId(Context paramContext) {
        String localObject = SharedPreferencesUtil.getCurrentDeviceId();
        if (localObject == null) {
            String str1 = Settings.Secure.getString(paramContext.getContentResolver(), "android_id");
            if ((str1 == null) || (str1.trim().isEmpty()) || ("9774d56d682e549c".equalsIgnoreCase(str1)))
            {
                String str2 = Build.SERIAL;
                if ((str2 != null) && (!str2.trim().isEmpty())) {
                    str1 = Build.SERIAL;
                } else {
                    str1 = UUID.randomUUID().toString();
                }
            }
            if ("unknown".equals(str1))
            {
                String str3 = Build.SERIAL;
                if ((str3 != null) && (!str3.trim().isEmpty())) {
                    str1 = Build.SERIAL;
                } else {
                    str1 = UUID.randomUUID().toString();
                }
            }
            secondDeviceId = str1;

10-05 21:31