尝试创建一个 Adapter 类来根据固件版本选择 .Java 文件

发布于 2024-09-16 14:10:00 字数 785 浏览 3 评论 0原文

我所做的是创建两个 .java 文件。一个可以在 1.5 手机 (SDK3) 上编译和运行,另一个可以在 2.0(SDK5) 上运行。因此,在本示例中,我将 1.5 文件称为“ExampleOld”,将新文件称为“Example”。我想知道我是否只是制作了这样的活动,如果它的工作方式有点像“门户”,并根据 SDK 选择要加载的活动,这样就不会出现崩溃或编译错误。我应该对我的代码进行任何更改吗?也许任何人以前都必须这样做。谢谢!

package com.my.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

public class ExamplePortal extends Activity { 
    int sdk=new Integer(Build.VERSION.SDK).intValue();

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);

    if (sdk<5) {
        Intent v = new Intent(this, ExampleOld.class);
        startActivity(v);
    }

    else {
        Intent v = new Intent(this, Example.class);
        startActivity(v);
    } 
}

}

What I did was create two .java files. One that can compile and run on a 1.5 phone (SDK3) and then one that works on 2.0(SDK5) So for this example i'll call the 1.5 file ExampleOld and the new one Example. I was wondering if i just made activity like this if it would work sort of like a "portal" and pick the activity to load depending on the SDK so there is no crash or compile errors. Are there any changes I should make to my code? Maybe anyone out there that's had to do this before. thanks!

package com.my.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

public class ExamplePortal extends Activity { 
    int sdk=new Integer(Build.VERSION.SDK).intValue();

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);

    if (sdk<5) {
        Intent v = new Intent(this, ExampleOld.class);
        startActivity(v);
    }

    else {
        Intent v = new Intent(this, Example.class);
        startActivity(v);
    } 
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼角的笑意。 2024-09-23 14:10:00

你正在做的事情(如果我错了请纠正我)是尝试保持向后兼容性,同时如果用户运行的是较新的 Android 版本,则使用新的 API。最好的方法是遵循 Google 发布的教程 此处。这避免了任何验证问题,并且确实是做事情的最佳方式。

What you're doing (correct me if I'm wrong) is trying to maintain backwards compatibility while making use of new APIs if the user is running a newer android version. The best way to do this is to follow the tutorial Google posted here. This avoids any verification issues and is really the best way to do stuff imho.

甜宝宝 2024-09-23 14:10:00

我会将这个决定放在工厂类中,以避免在代码库中出现这些 if-else 语句。

I would put this decision in a Factory Class to avoid having these if-else statements all over the codebase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文