简体   繁体   中英

android - SDK 4.0 on android 2.1

Even though I have done some app on android, I am still confused. Is it possible to use functions in SDK 4.0, and run the app on android 2.1 or lower?

I tried methods you guys mentioned but got an error -

Field requires API level 11 (current min is 7): android.os.AsyncTask#THREAD_POOL_EXECUTOR, if I change min to 11, the app can't install on android 2.1, so even I can use higher API, but it still can't run on android lower version...how to fix that?

From Kzinch's advice, set TargetApi to 11, then it's working!

If you want a program that runs on both SDK4 and SDK 2.1 you have two possibilities. One is to provide alternative implementations on your code when they are needed ie, if some function from SDK4 is not available in the SDK2.1 then you add a conditional block to your code that check the SDK version and provide code for each branch.

The other possibility is to use the Android Support Libaries in order to use the same code for both SDKs (no conditional blocks required). If you need a function provided by the SDK4 but not for the SDK2.1 you can check if that function is provided by a support library. If it is you can use it and your code will run fine on both SDK4 and SDK2.1 without requiring any version checking. For instance, if you need to use the LruCache class which is available since API level 12 (and so not available on SDK2.1) you can use the v4 support library which provide that function and works on SDK2.1 and SDK4. So in your code you would use

import android.support.v4.util.LruCache;

instead of

import android.util.LruCache;

Yes, you can use functions from the higher API in your code, but you must make sure they are never called on the lower API in runtime.

You should make checks for API level in runtime and provide alternative implementation that exists for that API level.

Let me provide some simple example:

    SharedPreferences.Editor edit = PreferenceManager
            .getDefaultSharedPreferences(getActivity()).edit();
    edit.putInt(KEY, VALUE);
    if (Build.VERSION.SDK_INT >= 9) {
        edit.apply();
    } else {
        edit.commit();
    }

apply() method is a faster (asynchronous) implementation of commit() method, but it not supported on the API level less than 9. With the help of API version check it all works perfect for all devices.

Update @TargetApi annotaion may be used to suppress Lint warnings/errors for API checks.

it doesn't matter what SDK level you compile your code against. Important is which methods/classes are you calling/instantiating.

If you use any newer classes or methods your code WILL crash running on older devices. The suggested method to deal with it is Lazy Loading: http://android-developers.blogspot.co.uk/2010/07/how-to-have-your-cupcake-and-eat-it-too.html

and remember, I'm saying this about the SDK.

The compatibility pack is a library developed by google that you can add to any project and use the functions of the library without issues.

Furthermore, there're 3rd party libraries (such as the ActionBar Sherlock http://actionbarsherlock.com/ that aims to bring higher SDK level functionalities to lower SDK levels)

No. You cannot use methods from higher API, because the code to handle it is simply not present on lower version of API. You can, however target as high API version as possible, but you may take care to call these methods on right API. You can easily check that at runtime with. ie

f( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
     // code to be run on Honeycomb and higher versions
}

如果您使用的是特定于更高版本的API,则该应用将无法在旧版本中运行。由于未在旧版本中定义,因此会引发错误。在AndroidManifest.xml中。

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