[英]Java Android - Defining Imports
I have following problem: I do have a class, common.java, which will be used by an Android application but also by a Java application. 我有以下问题:我确实有一个common.java类,该类将由Android应用程序使用,但也将由Java应用程序使用。 This class imports an android specific class (android.*). 此类导入android特定的类(android。*)。
If I want to build the Java application it does throw the error of a not existing import, of course. 当然,如果我要构建Java应用程序,则确实会引发不存在的导入错误。 Programmatically I do all my Android specific stuff in an if clause: 我以编程方式在if子句中完成了所有Android特定的工作:
if (System.getProperty("java.vm.name").equalsIgnoreCase("Dalvik")) {
//Android specific stuff
}
So: Is there a way, that I can import the Android classes also only if i'm building the android app? 所以:有一种方法,只有在构建android应用程序时,我才能导入Android类吗? Like an if clause around the imports? 就像关于导入的if子句一样?
if(System.getProperty("java.vm.name").equalsIgnoreCase("Dalvik")) {
import ...
import ...
}
Hopefully my problem is clear. 希望我的问题很清楚。 Thanks!! 谢谢!!
you have to splitt common into portable and os-specific code like this: 您必须将通用分为可移植和特定于操作系统的代码,如下所示:
// has no android or j2se specific code
public class common {
public void someCommonfunction() {...someOsSpecificcode(); ...}
protected abstract void someOsSpecificcode();
}
// has android specific code
public class commonAndroid extends common {
protected void someOsSpecificcode() { /* android specific implementation */ }
}
// has j2se specific code
public class commonJ2se extends common {
protected void someOsSpecificcode() { /* j2se specific implementation */ }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.