开发者

How to downgrade my android app from 2.2 to 1.5 sdk seamlessly

I have an android app which I started write on sdk 2.2, but my android phone has the 1.5 version of the android. I have to downgrade my app to 1.5 seamlessly. I am not fully s开发者_开发技巧ure but I didn't use too many 2.2-specific features. Can you tell me how to do it?


If you're using Eclipse, change the manifest's minSdkVersion and targetSdkVersion and project's setup to use 1.5 and anything that's not present in that version of the API will turn into an error.


Essentially follow Jean's link from above to the dev blog and take those recommendations. You're going to create a singleton class that lazy loads the right class for the corresponding API level of the device its running on. Functions that are unavailable in a version should handle that use case.

public abstract class StaticAct {

protected static StaticAct INSTANCE = null;

public static StaticAct getInstance() {
    final int sdkVersion = Integer.parseInt(Build.VERSION.SDK);

    if(INSTANCE==null)
    if(sdkVersion < Build.VERSION_CODES.DONUT){
        INSTANCE = new CupcakeStaticAct();
    }else if (sdkVersion < Build.VERSION_CODES.ECLAIR){
        INSTANCE = new DonutStaticAct();
    }else if(sdkVersion < Build.VERSION_CODES.FROYO){
        INSTANCE = new EclairStaticAct();
    }else if(sdkVersion < Build.VERSION_CODES.GINGERBREAD){
        INSTANCE = new FroyoStaticAct();
    }else{
        INSTANCE = new GingerbreadStaticAct();
    }

    return INSTANCE;
}

// Private constructor prevents instantiation from other classes
  protected StaticAct() {}
}

This abstract class then will have some abstract methods it defines like

public abstract boolean enableStrictMode();

A class can be defined for each api level. EDIT: these are all private classes defined inside of the StaticAct.java file. This allows the lazy loading to work correctly and prevent instantiation from other classes.

 /*
  * API Level 3
  */
 private static class CupcakeStaticAct extends StaticAct

The base class (extending the StaticAct) must implement all methods defined in StaticAct. If the method is unavailable for that api level then handle that case, throw an error or return false (or ignore it completely).

        @Override
        public void enableStrictMode() {
            //unavilable in cupcake, do nothing
        }

As your classes increase in api level they only need to implement methods that have changed from previous versions. So the multitouch APIs became available in 2.0, strict mode in 2.3 etc.


In your manifest.xml file:

<uses-sdk android:minSdkVersion="integer" 
          android:targetSdkVersion="integer"
          android:maxSdkVersion="integer" />

See here for detailed information

The API level is inserted into the minSdkVersion, targetSdkVersion and maxSdkVersion fields (not that maxSdkVersion has been deprecated in the toolkit, but will work in the market). To synopsize:

Platform       API Level
Android 2.3   9
Android 2.2   8
Android 2.1   7
Android 2.0.1 6
Android 2.0   5
Android 1.6   4
Android 1.5   3
Android 1.1   2
Android 1.0   1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜