如何应对日precated类在Android中,以保持兼容性兼容性、对日、precated、Android

2023-09-12 07:11:41 作者:习惯一个人

我又回到了工作的一个应用程序,我的工作了一段时间前,当我拥有了一切围绕的Andr​​oid 2.2 Froyo。

I am getting back to work on an app I worked on a while ago, when I had everything built around Android 2.2 Froyo.

我已经更新我的SDK最新的API和注意到ClipboardManager功能,我使用的是pcated德$ P $。我更新了code使用较新的ClipData模型,并尝试了一下我的升级Froyo的手机,当然,我得到了新的code一个NoClassDefFoundError的。

I have updated my SDK for the latest APIs and noticed that the ClipboardManager features I was using are deprecated. I updated the code to use the newer ClipData model and tried it out on my Froyo phone and, of course, I get a NoClassDefFoundError in the new code.

我有一个环顾四周SO,并没有发现总体战略的任何真正的讨论,以保持向后兼容性。

I've had a look around SO and haven't found any real discussions of general strategy for maintaining backwards compatibility.

我不完全相信我应该如何处理这个问题和其他情况下的API不同,因为我希望所有版本的用户能够使用我的应用程序。

I'm not entirely sure how I should handle this and other situations where the API differs, because I want users of all versions to be able to use my app.

我应该做一个检查的过程如下?

Should I be doing a check as follows?

if(version == old){
   use old API;
} else {
   use new API;
}

如果是这样,我也去precated code。在我的课堂和Eclipse将有警告永远存在。

If so, I have deprecated code in my class and Eclipse will have the warning there forever.

在另一方面,我可以只建立针对旧版本的API,并希望新的版本将处理它好。但后来我跑建设,防止出错或低性能code,当一个更好的选择可用的危险。

On the other hand, I could just build against an old version of the API and hope that new versions will handle it okay. But then I run the risk of building against buggy or low-performance code when a better alternative is available.

什么是解决这一问题的最佳方法是什么?

What is the best way to deal with this?

推荐答案

您可以做到这一点(检查API的版本)。

You can do that (checking the API version).

您也可以使用反射来调用新的类。

You can also use reflection to call the newer classes.

我就不会担心使用去precated方法,因为所有的Andr​​oid版本是向后兼容的,说你想看的东西的时候是3.0蜂窝,因为这些有一点不同。

I wouldn't worry about using deprecated methods as all Android versions are backwards compatible, saying that you want to watch when things are for 3.0 Honeycomb as these are a little different.

下面是如何使用反射的解释:(是的,它一直在此之前,所以也许搜索反射)

Here's an explanation of how to use reflection: (yes it's been on SO before, so maybe search for reflection)

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

我期待在做这个项目,这是可用但在那之前这里的一些code:

I'm looking at making the project this is in available but until then here's some code:

 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");
            }
    } 

现在getExternalFilesDir()仅适用于API级别8级以上,所以我想用这个,如果他们有(升级Froyo),但除此之外,我需要另一种方法。

Now getExternalFilesDir() is only available on API level 8 or above, so I want to use this if they have (Froyo), but otherwise I need another method.

现在我有我的测试方法,我可以继续前进,并尝试使用它:

Now I have my test for the method I can go ahead and attempt to use it:

  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 ;-))
  }

希望帮助和快捷方式很多使用Google: - )

Hope that helps and shortcuts a lot of googling :-)

P.S。如果在其他你想还是用你去prectated方法,只需添加上述 @燮pressWarnings(德precation)注释它,这将摆脱的警告,你已经为你的使用做了正确的原因最新的API在可能的情况。

P.S. if in the else you want to use your deprectated methods still, just add the @SuppressWarnings("deprecation") annotation above it, This will get rid of the warning and you have done it for the right reasons as you are using the latest API's when possible.