3

Imagine that I have a camera app where I show a small MapView in one corner showing where is the device located. So I have to extend my activity from a MapActivity to do that.

Then imagine that I want to release my app in a device without Google Apis, like the Kindle, for instance. As the MapActivity does not exist anymore, I should extend from a regular Activity.

I know that I can set

<uses-library
        android:name="com.google.android.maps"
        android:required="false" />

To let the user install the app even if the library is missing. And then use

    try {
        Class.forName("com.google.android.maps.MapView");
        googleMapsEnabled = true;
    } catch( ClassNotFoundException e ) {
        googleMapsEnabled = false;
    }

to check if the MapView class is present or not.

But the problem is the base class that I extend from, it should be Activity instead of MapActivity if Google Maps are not available.

My question is, how can I use the same code to support both options, without duplicating the whole code into different classes depending on the installed libs?

4

1 に答える 1

0

価値のあることとして、このようなものでの私の通常のアプローチ(つまり、異なるシナリオで2つの異なる基本クラスから継承する必要あるが、最終的には同じ(またはほぼ同じ)動作が必要な場合)は、私と同じくらい多くのことを実行することです。両方のクラスから呼び出す静的ヘルパークラスに共通の機能を組み込むことができます。

于 2012-10-24T12:56:30.650 に答える