Maximale GPS Geschwindigkeit speichern und noch eine Frage

J

Jajobe

Erfahrenes Mitglied
14
Guten Tag zusammen,
Ich möchte gern ein Update für meine App veröffentlichen, nur stehe ich vor zwei Fragen/Problemen.
Und zwar habe ich eine Tachoapp programmiert, die im Moment nur die Geschwindigkeit in Digital Zahlen anzeigt. Jetzt meine Fragen:
1. Ich möchte gern die Höchstgeschwindigkeit speichern, die der User der App erreicht hat. Wie ist das möglich?

2. Die Darstellung aktuelle Geschwindigkeit möchte ich gern grafisch aufpeppen, also aus den den Digitalzahlen soll ein analoger Tacho wie bei einem Auto werden.
Ich habe im Internet auch die Lib "Gaug View" gefunden, allerdings weiß ich nicht wie ich diese integrieren kann.

Hat einer von euch Antworten auf meine Fragen.


Code Teil 1:

Code:
package com.bjcreative.tachometer;

import java.math.BigDecimal;
import java.math.RoundingMode;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
 
public class MainActivity extends Activity implements GPSCallback{
    private GPSManager gpsManager = null;
    private double speed = 0.0;
    private int measurement_index = Constants.INDEX_KM;
    private AbsoluteSizeSpan sizeSpanLarge = null;
    private AbsoluteSizeSpan sizeSpanSmall = null;
    
    
    
   
 
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
	this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    
    
    
    
    
    
     
    gpsManager = new GPSManager();
     
    gpsManager.startListening(getApplicationContext());
    gpsManager.setGPSCallback(this);
     
    ((TextView)findViewById(R.id.info_message)).setText(getString(R.string.info));
     
    measurement_index = AppSettings.getMeasureUnit(this);
    
    
    
    
    final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

    if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
        buildAlertMessageNoGps();}}
    

  private void buildAlertMessageNoGps() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Your GPS seems to be disabled, do you want to enable it? (After enabling GPS restart the app.")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                   startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                    dialog.cancel();
               }
           });
    final AlertDialog alert = builder.create();
    alert.show();

}
 
    @Override
    public void onGPSUpdate(Location location) 
    {
            location.getLatitude();
            location.getLongitude();
            speed = location.getSpeed();
             
            String speedString = "" + roundDecimal(convertSpeed(speed),2);
            String unitString = measurementUnitString(measurement_index);
             
            setSpeedText(R.id.info_message,speedString + " " + unitString);
    }
 
    @Override
    protected void onDestroy() {
            gpsManager.stopListening();
            gpsManager.setGPSCallback(null);
             
            gpsManager = null;
             
            super.onDestroy();
    }
             
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
         
            return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
            boolean result = true;
     
            switch(item.getItemId())
            {
                    case R.id.menu_about:
                    {
                            displayAboutDialog();
                             
                            break;
                    }
                    case R.id.unit_km:
                    {
                            measurement_index = 0;
                             
                            AppSettings.setMeasureUnit(this, 0);
                             
                            break;
                    }
                    case R.id.unit_miles:
                    {
                            measurement_index = 1;
                             
                            AppSettings.setMeasureUnit(this, 1);
                             
                            break;
                    }
                    default:
                    {
                            result = super.onOptionsItemSelected(item);
                             
                            break;
                    }
            }
             
            return result;
    }
 
    private double convertSpeed(double speed){
            return ((speed * Constants.HOUR_MULTIPLIER) * Constants.UNIT_MULTIPLIERS[measurement_index]); 
    }
     
    private String measurementUnitString(int unitIndex){
            String string = "";
             
            switch(unitIndex)
            {
                    case Constants.INDEX_KM:                string = "km/h";        break;
                    case Constants.INDEX_MILES:     string = "mi/h";        break;
            }
             
            return string;
    }
     
    private double roundDecimal(double value, final int decimalPlace)
    {
            BigDecimal bd = new BigDecimal(value);
             
            bd = bd.setScale(decimalPlace, RoundingMode.HALF_UP);
            value = bd.doubleValue();
             
            return value;
    }
     
    private void setSpeedText(int textid,String text)
    {
            Spannable span = new SpannableString(text);
            int firstPos = text.indexOf(32);
             
            span.setSpan(sizeSpanLarge, 0, firstPos,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            span.setSpan(sizeSpanSmall, firstPos + 1, text.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
             
            TextView tv = ((TextView)findViewById(textid));
             
            tv.setText(span);
    }
     
    private void displayAboutDialog()
    {
            final LayoutInflater inflator = LayoutInflater.from(this);
            final View settingsview = inflator.inflate(R.layout.about, null);
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
             
            builder.setTitle(getString(R.string.app_name));
            builder.setView(settingsview);
                             
            builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                             
                    }
            });
                             
            builder.create().show();
    }
}


Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:keepScreenOn="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/info_message"
        android:layout_width="fill_parent"
        android:layout_height="144dp"
        android:layout_weight="0.42"
        android:background="@drawable/alu"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/info"
        android:textColor="#5EFF00"
        android:textSize="70sp"
        android:textStyle="bold" />


</LinearLayout>


Code Teil 2:

Code:
package com.bjcreative.tachometer;

import java.util.List;
 
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
 
public class GPSManager
{
        private static final int gpsMinTime = 500;
        private static final int gpsMinDistance = 0;
         
        private static LocationManager locationManager = null;
        private static LocationListener locationListener = null;
        private static GPSCallback gpsCallback = null;
         
        public GPSManager()
        {       
                GPSManager.locationListener = new LocationListener()
                {
                        @Override
                        public void onLocationChanged(final Location location)
                        {
                                if (GPSManager.gpsCallback != null)
                                {
                                        GPSManager.gpsCallback.onGPSUpdate(location);
                                }
                        }
                         
                        @Override
                        public void onProviderDisabled(final String provider)
                        {
                        }
                         
                        @Override
                        public void onProviderEnabled(final String provider)
                        {
                        }
                         
                        @Override
                        public void onStatusChanged(final String provider, final int status, final Bundle extras)
                        {
                        }
                };
        }
         
        public GPSCallback getGPSCallback()
        {
                return GPSManager.gpsCallback;
        }
         
        public void setGPSCallback(final GPSCallback gpsCallback)
        {
                GPSManager.gpsCallback = gpsCallback;
        }
         
        public void startListening(final Context context)
        {
                if (GPSManager.locationManager == null)
                {
                        GPSManager.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                }
                 
                final Criteria criteria = new Criteria();
                 
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                criteria.setSpeedRequired(true);
                criteria.setAltitudeRequired(false);
                criteria.setBearingRequired(false);
                criteria.setCostAllowed(true);
                criteria.setPowerRequirement(Criteria.POWER_LOW);
                 
                final String bestProvider = GPSManager.locationManager.getBestProvider(criteria, true);
                 
                if (bestProvider != null && bestProvider.length() > 0)
                {
                        GPSManager.locationManager.requestLocationUpdates(bestProvider, GPSManager.gpsMinTime,
                                        GPSManager.gpsMinDistance, GPSManager.locationListener);
                }
                else
                {
                        final List<String> providers = GPSManager.locationManager.getProviders(true);
                         
                        for (final String provider : providers)
                        {
                                GPSManager.locationManager.requestLocationUpdates(provider, GPSManager.gpsMinTime,
                                                GPSManager.gpsMinDistance, GPSManager.locationListener);
                        }
                }
        }
         
        public void stopListening()
        {
                try
                {
                        if (GPSManager.locationManager != null && GPSManager.locationListener != null)
                        {
                                GPSManager.locationManager.removeUpdates(GPSManager.locationListener);
                        }
                         
                        GPSManager.locationManager = null;
                }
                catch (final Exception ex)
                {
                         
                }
        }
}


Code Teil 3:

Code:
package com.bjcreative.tachometer;


 
public interface Constants 
{
        public static final int TEXT_SIZE_SMALL = 15;
        public static final int TEXT_SIZE_LARGE = 80;
        public static final int INDEX_KM = 0;
        public static final int INDEX_MILES = 1;
        public static final int DEFAULT_SPEED_LIMIT = 80;
        public static final int HOUR_MULTIPLIER = 3600;
        public static final double UNIT_MULTIPLIERS[] = { 0.001, 0.000621371192 };
}
 
Zuletzt bearbeitet:
Für die Höchstgeschwindigkeit kannst du einfach eine globale Variable setzen, die immer die bisherige Höchstgeschwindigkeit speichert.

In deiner ongpsupdate Methode hast du ja den momentanen speed. Da kannst du dann abfragen ob der höher als die bisher gespeicherte Höchstgeschwindigkeit ist, und wenn ja updatest du die Höchstgeschwindigkeit .
 
@Daaaaniel:
Warum soll die global sein?
 
ui_3k1 schrieb:
@Daaaaniel:
Warum soll die global sein?

Wenn sie lokal waere, wuerde sie ja bei jedem aufruf der methode zurückgesetzt. So könnte man nicht wissen ob die geschwindigkeit vorher schonmal hoeher war.
 
...
1. Ich möchte gern die Höchstgeschwindigkeit speichern, die der User der App erreicht hat. Wie ist das möglich?
Hm,
die Geschwindigkeiten streuen doch sehr stark und die Spitzenwerte sind grob falsch (jedenfalls bei meiner App).
D.h. Du musst Dir irgendeinen Mittelwert (oder eine Regressionsgerade) aus den letzten n Punkten bestimmen
(n ist nicht die Gesamtzahl der bisherigen Punkte, sondern deutlich kleiner).

Mit Gruß
E.S.
 

Ähnliche Themen

B
Antworten
6
Aufrufe
1.049
jogimuc
J
A
Antworten
10
Aufrufe
1.901
swa00
swa00
H
Antworten
2
Aufrufe
1.306
Hcman
H
Zurück
Oben Unten