Android Open Source - GoogleDirectionsClient Directions Service Activity






From Project

Back to project page GoogleDirectionsClient.

License

The source code is released under:

Apache License

If you think the Android project GoogleDirectionsClient listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.manuelpeinado.googledirectionsclient.demo;
/*from  w  w  w  .  ja v  a2s.  c o  m*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Window;
import com.manuelpeinado.googledirectionsclient.GoogleDirectionsResponse;
import com.manuelpeinado.googledirectionsclient.GoogleDirectionsResponseListener;
import com.manuelpeinado.googledirectionsclient.Query;
import com.manuelpeinado.googledirectionsclient.service.GoogleDirectionsService;

public class DirectionsServiceActivity extends SherlockFragmentActivity implements GoogleDirectionsResponseListener {

    private Button mButton;
    private Query mLastQuery;
    private LocalBroadcastManager mLocalBroadcastManager;
    private GeocodingReceiver mGeocodingReceiver = new GeocodingReceiver();

    private class GeocodingReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Query query = intent.getParcelableExtra(GoogleDirectionsService.PARAM_IN_QUERY);
            if (!query.equals(mLastQuery)) {
                return;
            }
            boolean success = intent.getBooleanExtra(GoogleDirectionsService.PARAM_OUT_SUCCESS, false);
            if (success) {
                GoogleDirectionsResponse response = intent
                        .getParcelableExtra(GoogleDirectionsService.PARAM_OUT_RESPONSE);
                onResponseReady(response);
            } else {
                onResponseReady(null);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mLastQuery = savedInstanceState.getParcelable("lastQuery");
        }
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
        setContentView(R.layout.activity_directions);
        mButton = (Button) findViewById(R.id.button);
        initButtonText();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable("lastQuery", mLastQuery);
    }

    private void initButtonText() {
        if (mLastQuery != null) {
            mButton.setText(android.R.string.cancel);
            setProgressBarIndeterminateVisibility(true);
        } else {
            mButton.setText(R.string.get_directions);
            setProgressBarIndeterminateVisibility(false);
        }
    }

    public void getDirections(View v) {
        if (mLastQuery != null) {
            cleanUpAfterResponse();
        } else {
            startDirectionsService();
            setProgressBarIndeterminateVisibility(true);
            mButton.setText(android.R.string.cancel);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter(GoogleDirectionsService.RESPONSE_ACTION);
        mLocalBroadcastManager.registerReceiver(mGeocodingReceiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mLocalBroadcastManager.unregisterReceiver(mGeocodingReceiver);
    }

    private void startDirectionsService() {
        GoogleDirectionsService.IntentBuilder intentBuilder = new GoogleDirectionsService.IntentBuilder();
        intentBuilder.setOrigin(Common.MADRID_LAT, Common.MADRID_LNG);
        intentBuilder.setDestination(Common.BARCELONA_LAT, Common.BARCELONA_LNG);
        mLastQuery = intentBuilder.getQuery();
        Intent intent = intentBuilder.build(this);
        startService(intent);
    }

    @Override
    public void onResponseReady(GoogleDirectionsResponse response) {
        cleanUpAfterResponse();
        if (response != null) {
            String fmt = "The trip from Madrid to Barcelona takes: %s";
            String text = String.format(fmt, response.getDuration());
            Toast.makeText(this, text, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, R.string.get_directions_error, Toast.LENGTH_LONG).show();
        }
    }

    private void cleanUpAfterResponse() {
        mLastQuery = null;
        mButton.setText(R.string.get_directions);
        setProgressBarIndeterminateVisibility(false);
    }

}




Java Source Code List

com.manuelpeinado.googledirectionsclient.AddressFormatter.java
com.manuelpeinado.googledirectionsclient.Distance.java
com.manuelpeinado.googledirectionsclient.Duration.java
com.manuelpeinado.googledirectionsclient.GoogleDirectionsClient.java
com.manuelpeinado.googledirectionsclient.GoogleDirectionsResponseListener.java
com.manuelpeinado.googledirectionsclient.GoogleDirectionsResponse.java
com.manuelpeinado.googledirectionsclient.Leg.java
com.manuelpeinado.googledirectionsclient.Polyline.java
com.manuelpeinado.googledirectionsclient.Query.java
com.manuelpeinado.googledirectionsclient.RoutePoint.java
com.manuelpeinado.googledirectionsclient.Route.java
com.manuelpeinado.googledirectionsclient.Step.java
com.manuelpeinado.googledirectionsclient.demo.ActivityInfo.java
com.manuelpeinado.googledirectionsclient.demo.Common.java
com.manuelpeinado.googledirectionsclient.demo.DirectionsFragmentActivity.java
com.manuelpeinado.googledirectionsclient.demo.DirectionsServiceActivity.java
com.manuelpeinado.googledirectionsclient.demo.HomeActivity.java
com.manuelpeinado.googledirectionsclient.fragment.GoogleDirectionsFragment.java
com.manuelpeinado.googledirectionsclient.service.GoogleDirectionsService.java