Add statements for higher version?

大憨熊 提交于 2019-12-06 08:23:29

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 */
}
if (Build.VERSION.SDK_INT > 7) {
    ...
}

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;
    }
BadSkillz
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!