简体   繁体   中英

Android Read from Settings.Global from API < 17?

Edit per the answer below I changed manifest to :

<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17" />

Created method:

@TargetApi(17)
private static boolean isAirplaneModeOn17(Context context) {
   return Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
}

However I'm still getting "Settings.Global cannot be resolved or is not a field" when I clean the project. Ideas?


I have an app where I read airplane mode state using the following code:

return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;

This works great for all my GB and <= 4.1x uses.

Of course, in API 17 Settings.Global was introduced and this location has changed.

return boolean airmode = Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 1;

Aside from the fact there could be workarounds to get airplane mode state in API17, the real question is how does one compile a project that supports both reading of Settings.System and Settings.Global from the same project?

I can't seem to be able to make Eclipse "ignore" that "Settings.Global cannot be resolved or is not a field" when the project build target is set to anything other than 4.2 API 17. I know the code will work for my 4.2 users, and have that designation setup as follows:

private static boolean isAirplaneModeOn(Context context) {
    if (apiVersion < 17) {
       return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
    } else {
       return Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
    }
}

I've messed around with min and target API in the manifest and it doesn't seem to have any affect, Eclipse still will not compile my project.

FWIW, here's some pertinent version info:

ADK 21.0.0.v201210310015-519525

Eclipse Version: 4.2.1 Build id: M20120914-1800

TIA!

右键单击您的项目,单击Properties,然后单击Android,检查Android 4.2,然后Eclipse将加载4.2所需的所有内容,并且所有内容都应该编译。

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