如何支持不同的android OS?

时间:2014-02-25 10:32:07

标签: java android android-asynctask

我有一些问题 我有两种方法,一种用Api 11及以上调用,另一种用于旧api。

我的项目有一些设置:

项目构建目标 - Google API 2.3.3

清单文件:

uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="18" 

我的代码:

abstract class DoAsyncRequest {
    protected abstract void DoRequest(Connection conn) throws Exception;
}

class DoAsyncRequestLegacy extends DoAsyncRequest {
    protected void DoRequest(Connection conn) throws Exception{
        new FetchTask(conn).execute();// FOR OLD API
    }
}


@TargetApi(11) class DoAsyncRequest_SDK11 extends DoAsyncRequest {
    protected void DoRequest(Connection conn) throws Exception{
        new FetchTask(conn).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);                    //FOR NEW API, but compiller says, that  THREAD_POOL_EXECUTOR cannot be resolved or is not a field
    }
}

最好的问候。

2 个答案:

答案 0 :(得分:6)

通常我在以这种方式调用正确方法之前进行测试:

  // change with the version you want to test
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 

  } else {

  }

答案 1 :(得分:1)

 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 

     new FetchTask(conn).execute();// FOR OLD API

  } else {

     new FetchTask(conn).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);

  }