English 中文(简体)
引用库项目中的项目对象
原标题:Referencing project object from library project

I have a game I have built that will have 2 versions. One "lite" and one "full". For this purpose I put the large part of the code in a library-project.

Thing is that within part of the code I have to start an Activity whose code will be in the program project and not the library one. I found that it will not compile.

我解决这个问题的方法是为库项目中的活动创建一个虚拟类,但这可能不是正确的方法。

最佳回答

如果你说库项目的某些部分依赖于一个类,该类在一个应用程序项目中有一个实现,在另一个应用项目中可能有另一个实现或不存在,那么我怀疑一个简单的接口会有所帮助。

在库项目中声明一个接口,并更改该项目中对接口的所有引用。在应用程序项目中,提供实现该接口的不同类,并根据接口是精简还是完整来改变它们的行为。

我看不出对活动的依赖是如何表现出来的,因为在大多数情况下,你都是通过字符串通过ComponentName引用活动的。

举个小例子,假设我们希望能够在精简版中的视图组中注入广告横幅图像视图,但什么也不做,或者可能在完整版中隐藏视图组。

在库项目中,为AdBannerProvider声明一个接口:

public interface AdBannerProvider
{
    public void InjectAd(ViewGroup v);
}

在图书馆项目中可能需要广告的活动中(取决于照明或完整),请参考以下界面:

private void getAdBanner(AdBannerProvider pProvider, ViewGroup pAdView)
{
     pProvider.InjectAd(pAdView);
}

然后在Lite版本中,提供AdBannerProvider的实现,为该版本做正确的事情:

public class LiteAdBannerProviderImpl implements AdBannerProvider {
   @Override
   public void InjectAd(ViewGroup v) {
          // do something useful for the lite edition, like add an imageview to the passed viewgroup
   }
}

在完整版中,另一个类为该版本做了正确的事情:

public class FullAdBannerProviderImpl implements AdBannerProvider {

    @Override
    public void InjectAd(ViewGroup v) {
        // do something useful for the full edition, like maybe set the viewgroup to GONE or INVISIBLE
    }
}
问题回答

应用程序项目应该取决于库项目的组件/资源。如果活动/服务需要从库启动,那么可能会使用正确的组件集发布意图。

Intent intent=new Intent();
intent.setComponent(new ComponentName("com.pkg", "com.pkg.class"));
startActivity(intent); 




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签