use*_*448 1 android function library-project
我有一个Android应用程序项目.我创建了一个库项目并在应用程序项目中添加了引用.现在我需要调用/访问库项目中应用程序项目中的某些函数/类/方法.我怎样才能做到这一点 ?
小智 11
在库中创建一个接口,用于定义您希望库调用的函数.让应用程序实现接口然后用库注册实现对象.然后库可以通过该对象调用应用程序.
在库中,声明接口并添加注册功能:
public class MyLibrary {
public interface AppInterface {
public void myFunction();
}
static AppInterface myapp = null;
static void registerApp(AppInterface appinterface) {
myapp = appinterface;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的申请中:
public class MyApplication implements MyLibrary.AppInterface {
public void myFunction() {
// the library will be able to call this function
}
MyApplication() {
MyLibrary.registerApp(this);
}
}
Run Code Online (Sandbox Code Playgroud)
您的库现在可以通过AppInterface对象调用该应用程序:
// in some library function
if (myapp != null) myapp.myFunction();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1746 次 |
| 最近记录: |