簡體   English   中英

不兼容的類型。 必需:android.content.Context找到:java.lang.String

[英]Incompatible types. Required: android.content.Context Found: java.lang.String

我需要使用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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM