简体   繁体   中英

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

I need to get device id using Secure.getString()

I am using context

 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;

This line showing below error

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

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

As per @vlumi comment and my debugging I found following code to fix this. It is fixed now, just need to replace context with string.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM