如何在AndroidManifest.xml中指定包装类

时间:2017-08-02 02:06:16

标签: android xml android-studio

我在Android Studio中设置了标准的地图活动。然后我需要添加一个'wrapper'类,以便我可以实现一个与MapsActivity的OnMapReadyCallback不同的类。此包装类没有可视组件,不应创建视图,只需调用MapsActivity即可创建地图视图。

所有内容都会编译,但运行时不会显示任何视图。

如果我在创建地图后将戒备活动从dispose移动到MReady我只会在日志中获得以下行。

  

E / ConnectivityService:尝试发送一个RemoteException   NetworkRequest的回调消息[id = 10,legacyType = -1,[   能力:INTERNET& NOT_RESTRICTED& TRUSTED]]

但仍然没有看法。下面的代码更新到最新。此外,我的Log.d / i消息都没有出现在日志中,尽管它设置为详细。

代码 - 的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.booxotel.smallgminterface">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name = ".SmallMap">

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name="com.booxotel.smallgminterface.MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

SmallMap.java(部分)

public class SmallMap extends Application implements FREExtension {
    public static final String TAG = "MapInterface";
    private static GoogleMap gMap;
    private static LatLng cmap;
    private static MarksData md;
    private Handler mHandler;

    public static FREContext extensionContext;
    public static Context appContext;
    public static SettingsContentObserver mSettingsWatcher;

    public ShowMap shm;

    @Override
    public void onCreate() {
        super.onCreate();
        appContext = getApplicationContext();
    }

    public class ShowMap extends Thread {
        ShowMap() {
            prepareThread(false);
        }

        public void prepareThread(Boolean quit){
            Log.i(TAG, "prepare thread");
            if (gMap == null && !quit) {
                Looper.prepare();
                mHandler = new Handler();
                Intent intent = new Intent(SmallMap.appContext, MapsActivity.class);
                appContext.startActivity(intent);
            }else if(quit) {
                Looper.myLooper().quit();
            }
            Looper.loop();
        }
    }

    public class MReady implements MapReadyEvent {
        @Override
        public void MapReady() {
            gMap = MapsActivity.getMap();
            shm.prepareThread(true);
            Log.i(TAG, "map ready");
            extensionContext.dispatchStatusEventAsync("mapReady", "true");
        }
    }

    @Override
    public FREContext createContext(String contextType) {
        shm = new ShowMap();
        shm.prepareThread(false);
        return new MapInterfaceContext();
    }

    @Override
    public void dispose() {
        Log.d(TAG, "Extension disposed.");
        Context context = appContext.getApplicationContext();
        context.getContentResolver().unregisterContentObserver(mSettingsWatcher);
        appContext = null;
        extensionContext = null;
        mSettingsWatcher = null;
    }

    @Override
    public void initialize() {
        Log.d(TAG, "Extension initialized.");
    }
}

MapsActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    public static GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        MapReadyEvent mre = new MapReadyEvent() {
            @Override
            public void MapReady() {}
        };
        mre.MapReady();
    }

    public static GoogleMap getMap() {
        return mMap;
    }
}

MapReadyEvent.java

public interface MapReadyEvent {
    void MapReady();
}

activity_maps.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="311dp"
        android:layout_height="290dp"
        android:layout_marginLeft="710dp"
        android:layout_marginTop="167dp"
        tools:context="com.mysite.testmap.MapsActivity" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

主线程上未调用startActivity。 请改用:

mHandler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
    @Override
    public void run() {
        Intent intent = new Intent(SmallMap.appContext, MapsActivity.class);
        appContext.startActivity(intent);
    }
});