Trying to make a Adapter class to choose .Java file depending on firmware version
What I did was create two .java files. One that can compile and r开发者_如何学运维un 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);
}
}
}
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.
I would put this decision in a Factory Class to avoid having these if-else statements all over the codebase.
精彩评论