如何处理Android中已弃用的类以保持兼容性

HXC*_*ine 30 compatibility android backwards-compatibility deprecated

我正在重新开始研究我之前工作的应用程序,当时我已经围绕Android 2.2 Froyo构建了所有内容.

我已经更新了我的SDK以获取最新的API,并注意到我使用的ClipboardManager功能已被弃用.我更新了代码以使用较新的ClipData模型并在我的Froyo手机上试用了它,当然,我在新代码中得到了一个N​​oClassDefFoundError.

我已经浏览了一下,并没有找到任何关于保持向后兼容性的一般策略的真实讨论.

我不完全确定我应该如何处理这个以及API不同的其他情况,因为我希望所有版本的用户都能够使用我的应用程序.

我应该按如下方式进行检查吗?

if(version == old){
   use old API;
} else {
   use new API;
}
Run Code Online (Sandbox Code Playgroud)

如果是这样,我已经弃用了我的类中的代码,Eclipse将永远发出警告.

另一方面,我可以构建一个旧版本的API,并希望新版本可以正常处理它.但是当有更好的替代方案时,我冒着构建有缺陷或低性能代码的风险.

处理这个问题的最佳方法是什么?

Blu*_*ell 17

你可以这样做(检查API版本).

您还可以使用反射来调用较新的类.

我不担心使用已弃用的方法,因为所有Android版本都向后兼容,并表示您希望观察3.0 Honeycomb的用途,因为它们有点不同.

这里有一个如何使用反射的解释:(是的,它之前已经过了,所以也许可以搜索反射)

http://www.youtube.com/watch?v=zNmohaZYvPw&feature=player_detailpage#t=2087s

我正在考虑使这个项目可用,但在此之前这里是一些代码:

(您可以在扩展应用程序的类中执行此操作,即一次设置)

 public static Method getExternalFilesDir;

    static {
            try {
                    Class<?> partypes[] = new Class[1];
                    partypes[0] = String.class;
                    getExternalFilesDir = Context.class.getMethod("getExternalFilesDir", partypes);
            } catch (NoSuchMethodException e) {
                    Log.e(TAG, "getExternalFilesDir isn't available in this devices api");
            }
    } 
Run Code Online (Sandbox Code Playgroud)

现在getExternalFilesDir()仅在API级别8或更高版本上可用,所以如果他们有(Froyo)我想使用它,但我需要另一种方法.

现在我测试了我可以继续尝试使用它的方法:

  if(ClassThatExtendsApplication.getExternalFilesDir != null){
            Object arglist[] = new Object[1];
            arglist[0] = null;  
            File path = (File) ClassThatExtendsApplication.getExternalFilesDir.invoke(context, arglist);
           // etc etc
  } else {
      // Not available do something else (like your deprecated methods / or load a different class / or notify they should get a newer version of Android to enhance your app ;-))
  }
Run Code Online (Sandbox Code Playgroud)

希望有很多谷歌搜索的帮助和捷径:-)

PS如果在其他地方你仍然想要使用你的去除方法,只需添加@SuppressWarnings("deprecation") 它上面的注释,这将消除警告,你已经完成了正确的原因,因为你正在使用最新的API.


Jor*_*sys 5

下面是一个例子:

import android.os.Build;

public static int getWidth(Context mContext){
    int width=0;
    WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    if(VERSION.SDK_INT > VERSION_CODES.HONEYCOMB){                   
        Point size = new Point();
        display.getSize(size);
        width = size.x;
    } 
    else{ 
        width = display.getWidth();  // deprecated, use only in Android OS<3.0.
    } 
    return width;
} 
Run Code Online (Sandbox Code Playgroud)

正如您所看到的代码部分:

  if(VERSION.SDK_INT > VERSION_CODES.HONEYCOMB){                   
            Point size = new Point();
            display.getSize(size);
            width = size.x;
        } 
Run Code Online (Sandbox Code Playgroud)

仅适用于 Android 3.0 及更高版本,如果您希望此代码至少可用于 Jelly Bean (Android 4.1),请使用:

  if(VERSION.SDK_INT > VERSION_CODES.JELLY_BEAN){                   
            Point size = new Point();
            display.getSize(size);
            width = size.x;
        } 
Run Code Online (Sandbox Code Playgroud)

VERSION.SDK_INT 用户可见的框架 SDK 版本;它的可能值在 Build.VERSION_CODES 中定义。

更多信息:Build.VERSION

你可以在这里看到VERSION_CODES常量:Build.VERSION_CODES