Android Open Source - android-observable-location Demo Activity






From Project

Back to project page android-observable-location.

License

The source code is released under:

MIT License

If you think the Android project android-observable-location 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.zmarkan.observablelocation.sample;
//from   w w w . ja v  a  2  s.com
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zmarkan.observablelocation.ObservableLocationProvider;
import com.zmarkan.observablelocation.ObservableLocationProviderImpl;

import java.util.Date;

import observablelocation.zmarkan.com.observablelocation.R;
import rx.Subscription;
import rx.functions.Action0;
import rx.functions.Action1;

public class DemoActivity extends Activity {
    
    Button getUpdatesButton;
    Button unsubscribeButton;
    Button getSingleUpdateButton;
    
    TextView latitudeTextView;
    TextView longitudeTextView;
    TextView lastUpdatedTextView;
    
    private Subscription locationUpdatesSubscription;
    private ObservableLocationProvider observableLocationProvider;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        observableLocationProvider = new ObservableLocationProviderImpl(this);
        
        getUpdatesButton = (Button) findViewById(R.id.button_get_updates);
        unsubscribeButton = (Button) findViewById(R.id.button_unsubscribe);
        getSingleUpdateButton = (Button) findViewById(R.id.button_get_single_update);
        latitudeTextView = (TextView) findViewById(R.id.textview_latitude);
        longitudeTextView = (TextView) findViewById(R.id.textview_longitude);
        lastUpdatedTextView = (TextView) findViewById(R.id.textview_last_updated);
        
        
        getUpdatesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                locationUpdatesSubscription = 
                        observableLocationProvider.provideLocationUpdates()
                                .subscribe(
                                        new LocationUpdatedAction(),
                                        new LocationErrorAction(),
                                        new LocationUpdatesCompleteAction());
            }
        });
        
        unsubscribeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(locationUpdatesSubscription != null){
                    locationUpdatesSubscription.unsubscribe();
                }
            }
        });
        
        getSingleUpdateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                locationUpdatesSubscription =
                        observableLocationProvider.provideSingleLocationUpdate()
                                .subscribe(
                                        new LocationUpdatedAction(),
                                        new LocationErrorAction(),
                                        new LocationUpdatesCompleteAction());
            }
        });
    }
    
    class LocationUpdatedAction implements Action1<Location>{
        @Override
        public void call(Location location) {
            Log.d(DemoActivity.this.getClass().getSimpleName(), "location");

            String lastUpdated = "Last updated: " + new Date().getTime()/1000;
            longitudeTextView.setText(String.valueOf(location.getLongitude()));
            latitudeTextView.setText(String.valueOf(location.getLatitude()));
            lastUpdatedTextView.setText(lastUpdated);
        }
    }
    
    class LocationErrorAction implements Action1<Throwable>{
        @Override
        public void call(Throwable throwable) {
            String lastError = "Error: " + new Date().getTime()/1000;
            lastUpdatedTextView.setText(lastError);
            Log.d(DemoActivity.this.getClass().getSimpleName(), throwable.getMessage());
        }
    }
    
    class LocationUpdatesCompleteAction implements Action0{
        @Override
        public void call() {
            Log.d(DemoActivity.this.getClass().getSimpleName(), "completed");
            String lastCompleted = "Completed: " + new Date().getTime()/1000;
            lastUpdatedTextView.setText(lastCompleted);
        }
    }
}




Java Source Code List

com.zmarkan.observablelocation.LocationUpdatesObservableTest.java
com.zmarkan.observablelocation.LocationUpdatesObservable.java
com.zmarkan.observablelocation.ObservableLocationProviderImpl.java
com.zmarkan.observablelocation.ObservableLocationProvider.java
com.zmarkan.observablelocation.sample.ApplicationTest.java
com.zmarkan.observablelocation.sample.DemoActivity.java