Google Map:我可以使用onNewIntent在FragmentActivity中接收数据吗?

时间:2015-07-10 14:33:41

标签: android google-maps android-fragments android-intent

我正在尝试将可序列化的arrayList作为意图传递给Map FragmentActivity但我面临的问题是在10-20秒之后调用onNewIntent方法会延迟传递数据的位置。我可以在onNewIntent中使用FragmentActivity接收数据吗?为什么在传递意图时需要时间来调用它?

我之前使用onCreate()方法创建了包含数据的列表,并且谷歌地图加载速度很快。

我感谢任何帮助。

我的IntenService中的代码:

                    Intent intent1 = new Intent(this, Map.class);
                    intent1.putExtra("list_data", data);
                    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent1);

地图活动:

public class Map extends FragmentActivity implements OnMapReadyCallback {
    GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (servicesOK()) {

            setContentView(R.layout.map);

        } else {
            setContentView(R.layout.map_main);

        }

    }

    public boolean servicesOK() {
        int isAvailable = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(this);
        if (isAvailable == ConnectionResult.SUCCESS) {
            return true;
        } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
                    this, 9001);
            dialog.show();

        } else {
            Toast.makeText(this, "Cant't connect to Google Play services",
                    Toast.LENGTH_SHORT).show();
        }
        return false;

    }

    private boolean initMap() {
        if (map == null) {
            SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            map = mapFrag.getMap();
        }
        return (map != null);
    }

    private void gotoLocation(double lat, double lng, String route_direct) {

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        final float zoom = 11;
        LatLng ll = new LatLng(lat, lng);
        if (lat != 0 && lng != 0 && !route_direct.isEmpty()) {
            MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
                    .position(ll).visible(true);

            Marker marker = map.addMarker(markerOpt);
            marker.showInfoWindow();
            CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
            map.moveCamera(update);
            Toast.makeText(this, "inside getoLocation()!", Toast.LENGTH_SHORT)
                    .show();

        }

    }

    @Override
    public void onMapReady(GoogleMap arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onNewIntent(Intent intent) {
        // TODO Auto-generated method stub
        super.onNewIntent(intent);

        @SuppressWarnings("unchecked")
        ArrayList<ItemDTO> list = (ArrayList<ItemDTO>) intent
                .getSerializableExtra("list_data");
        for (ItemDTO itemDTO : list) {
            double latitude = itemDTO.getLatitude();
            double longitude = itemDTO.getLongitude();
            int route1 = itemDTO.getRoute();
            String route = Integer.toString(route1);
            String direction = itemDTO.getDirection();
            String route_dirc = route + ", " + direction;

            if (initMap()) {
                Toast.makeText(this, "Ready to Map", Toast.LENGTH_SHORT).show();
                gotoLocation(latitude, longitude, route_dirc);

            } else {
                Toast.makeText(this, "Map not avialable", Toast.LENGTH_SHORT)
                        .show();
            }

        }
        Toast.makeText(this, "onNewIntent end!", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
    }

0 个答案:

没有答案