简体   繁体   中英

How to specify a wrapper class in AndroidManifest.xml

I have set up a standard Maps Activity in Android Studio. I then need to add a 'wrapper' class so that I can implement a class that is different from the MapsActivity's OnMapReadyCallback. This wrapper class has no visual components and should not create a View and simply then calls MapsActivity to create the map view.

Everything compiles, but when run no view appears.

If I move the quit looper from dispose to MReady just after map is created I only get the following line in the log.

E/ConnectivityService: RemoteException caught trying to send a callback msg for NetworkRequest [ id=10, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED] ]

But still no view appears. Below code updated to latest. Also none of my Log.d/i messages appear in the log although it's set to verbose.

Code - 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 (part of)

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>

The startActivity is not called on the main thread. Use this instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM