开发者

Add statements for higher version?

I am using 1.6 i.e. API 4 to build my application. There are couple of commands that are supported by higher versions. I want to write those commands and make application more compatible for higher versons. Like, I use Tabs. I want to use setLeftStripDrawable and setRightStripDrawable, but they are supported by API 8.

I write something like :

// I want these lines to come into affect only if the device SDK is greater than 7 as SDK of below 7, doesn't su开发者_JS百科pport these methods.
if (android.os.Build.VERSION.SDK_INT > 7) {   
    tw.setLeftStripDrawable(R.drawable.tab_selected_bar_left_v4);  // TabWidget
}

EDIT : I want to set setLeftStripDrawable to the tabs used in my app. In my manifest I have uses-sdk android:minSdkVersion="4". If I write the lines as above and compile it in 2.3, it compiles successfully. When I run in 1.6 I get "java.lang.VerifyError". If I remove those liens and again run in 1.6, it works properly.

What should I do to execute those lines only if the device SDK api is > 7, and if it is less than that then those lines should not come under any affect ?

Any clue ?


if (Build.VERSION.SDK_INT > 7) {
    ...
}


I think you should use something like this. I did this by heart, so there might be some errors.

try {
    Method twMethod = TabWidget.class.getMethod("setLeftStripDrawable", new Class[] { int.class });
    twMethod.invoke(tw, R.drawable.yourdrawable);
} catch (NoSuchMethodException e) {
    /* not supported */
} catch (IllegalArgumentException e) {
    /* wrong class provided */
} catch (IllegalAccessException e) {
    /* Java access control has denied access */
} catch (InvocationTargetException e) {
    /* method has thrown an exception */
}


You can try looking at Android Reflection. I have not used it yet myself, but as far as i understand, you can test for classes and methods that you know the name of. Then you can instantiate and use them.

You can read some of the basics here: http://www.tutorialbin.com/tutorials/85977/java-for-android-developers-reflection-basics


Here's some sample Android code, using reflection, that does something similar. It calls the getRotation() method from the Display class; the method only exists in SDK 8+. I've used it in one of my apps and it works:

    //I want to run this:  displayrotation = getWindowManager().getDefaultDisplay().getRotation();
    //but the getRotation() method only exists in SDK 8+, so I have to call it in a sly way, using Java "reflection"
    try{
        Method m = Display.class.getMethod("getRotation", (Class[]) null);//grab the getRotation() method if it exists
        //second argument above is an array containing input Class types for the method. In this case it takes no inputs.
        displayrotation = (Integer) m.invoke(getWindowManager().getDefaultDisplay(),(Object[]) null);
        //again, second argument is an array of inputs, in this case empty
    }catch(Exception e){//if method doesn't exist, take appropriate alternate actions
        Log.w("getRotation","old OS version => Assuming 90 degrees rotation");
        displayrotation = Surface.ROTATION_90;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜