Android Open Source - LocationUpdate Location Update






From Project

Back to project page LocationUpdate.

License

The source code is released under:

Copyright (c) 2011 Stefan A. van der Meer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to dea...

If you think the Android project LocationUpdate 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.meernet.LocationUpdate;
//from w w  w.  j a  v a2  s .c  o m
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Display and control activity for the SendService.
 *
 * @author svdm
 *
 */
public class LocationUpdate extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener {
    static final String TAG = "LocationUpdate";

    /**
     * Set up listeners.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button prefBtn = (Button) findViewById(R.id.to_preferences);
        prefBtn.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                        startActivity(new Intent(getBaseContext(), LocationUpdatePrefs.class));
                }
        });

        Button forceBtn = (Button) findViewById(R.id.force_update);
        forceBtn.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                        SendService.schedule(getBaseContext(), 1000, true);
                }
        });

        TextView logDisplay = (TextView) findViewById(R.id.log_display);
        logDisplay.setMovementMethod(ScrollingMovementMethod.getInstance());

        SharedPreferences prefs = getSharedPreferences(SendService.PREFS_NAME, 0);
        prefs.registerOnSharedPreferenceChangeListener(this);
    }

    /**
     * Load and show the latest location information.
     */
    private void showLastLocation() {
        SharedPreferences prefs = getSharedPreferences(SendService.PREFS_NAME, 0);

        // load previously sent location, if any
        float lastLat  = prefs.getFloat("lastLat", 0);
        float lastLon  = prefs.getFloat("lastLon", 0);
        long  lastTime = prefs.getLong("lastTime", 0);

        String lastTimeText = "No update sent yet.";
        if (lastTime != 0) {
            lastTimeText = DateUtils.formatDateTime(this, lastTime, DateUtils.FORMAT_SHOW_TIME + DateUtils.FORMAT_SHOW_DATE + DateUtils.FORMAT_ABBREV_ALL + DateUtils.FORMAT_24HOUR);
        }

        String lastLatText = String.format("% .3f", lastLat);
        String lastLonText = String.format("% .3f", lastLon);

        ((TextView) findViewById(R.id.lastsent_time)).setText(lastTimeText);
        ((TextView) findViewById(R.id.lastsent_lat) ).setText(lastLatText);
        ((TextView) findViewById(R.id.lastsent_lon) ).setText(lastLonText);

    }

    /**
     * Load and show log data, if it is enabled in prefs.
     */
    private void showLog() {
        SharedPreferences prefs = getSharedPreferences(SendService.PREFS_NAME, 0);

        boolean showLog = prefs.getBoolean("showLog", false);

        TextView log = (TextView) findViewById(R.id.log_display);
        if (showLog) {
            String logText  = prefs.getString("log", "No log messages.");
            log.setText(logText);

            log.setVisibility(View.VISIBLE);
        } else {
            log.setVisibility(View.GONE);
        }
    }

    /**
     * Load and show data.
     */
    @Override
    protected void onResume() {
        super.onResume();

        showLastLocation();
        showLog();
    }

    /**
     * Update data shown on screen immediately if the service stores changes.
     */
    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        if (key.equals("log")) {
            showLog();

        } else if (key.equals("lastTime")) {
            showLastLocation();

        } else if (key.equals("destination")) {
            // The destination URL is the only field that is freely editable,
            // so do some really quick validation here.
            try {
                new URL(prefs.getString(key, ""));
            } catch (MalformedURLException e) {
                Log.e(TAG, "User set invalid URL.");

                Toast.makeText(this, R.string.error_url_syntax, Toast.LENGTH_LONG).show();
            }
        }
    }

}




Java Source Code List

com.meernet.LocationUpdate.BootReceiver.java
com.meernet.LocationUpdate.LocationSendTask.java
com.meernet.LocationUpdate.LocationUpdatePrefs.java
com.meernet.LocationUpdate.LocationUpdate.java
com.meernet.LocationUpdate.SendService.java