add position update on "home"

This commit is contained in:
Matthias Schreiner 2019-06-23 18:36:05 +03:00
parent acda3e7328
commit 5c94c82895
4 changed files with 62 additions and 10 deletions

View File

@ -49,7 +49,7 @@ public class LocationService extends Service {
.putFloat("latitude", (float) locationResult.getLocations().get(0).getLatitude()) .putFloat("latitude", (float) locationResult.getLocations().get(0).getLatitude())
.putFloat("longitude", (float) locationResult.getLocations().get(0).getLongitude()) .putFloat("longitude", (float) locationResult.getLocations().get(0).getLongitude())
.putFloat("altitude", (float) locationResult.getLocations().get(0).getAltitude()) .putFloat("altitude", (float) locationResult.getLocations().get(0).getAltitude())
.putFloat("accuracy", (float) locationResult.getLocations().get(0).getAccuracy()) .putFloat("accuracy", locationResult.getLocations().get(0).getAccuracy())
.putFloat("speed", locationResult.getLocations().get(0).getSpeed()).apply(); .putFloat("speed", locationResult.getLocations().get(0).getSpeed()).apply();
} }
@ -102,7 +102,7 @@ public class LocationService extends Service {
notification = new NotificationCompat.Builder(this, CHANNEL_ID) notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentText("Updating location") .setContentText("Updating location")
.setContentTitle("DailyHelper Location Service") .setContentTitle("DailyHelper Location Service")
.setSmallIcon(R.drawable.ic_star) .setSmallIcon(R.drawable.ic_location_on)
.setContentIntent(pendingIntent).build(); .setContentIntent(pendingIntent).build();
} }

View File

@ -1,21 +1,67 @@
package at.fhj.swd.dailyhelper.ui.fragemnt; package at.fhj.swd.dailyhelper.ui.fragemnt;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView;
import at.fhj.swd.dailyhelper.R; import at.fhj.swd.dailyhelper.R;
import at.fhj.swd.dailyhelper.service.LocationService;
public class HomeFragment extends Fragment { public class HomeFragment extends Fragment {
private TextView tvLongitude;
private TextView tvLatitude;
private TextView tvSpeed;
private TextView tvAltitude;
private TextView tvAccuracy;
private Context context;
private Handler handler;
private SharedPreferences preferences;
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false); View view = inflater.inflate(R.layout.fragment_home, container, false);
context = this.getContext();
preferences = PreferenceManager.getDefaultSharedPreferences(this.getContext());
tvLongitude = view.findViewById(R.id.tv_longitude);
tvLatitude = view.findViewById(R.id.tv_latitude);
tvSpeed = view.findViewById(R.id.tv_speed);
tvAltitude = view.findViewById(R.id.tv_altitude);
tvAccuracy = view.findViewById(R.id.tv_accuracy);
// Create the Handler object (on the main thread by default)
handler = new Handler();
// Define the code block to be executed
Runnable runnableCode = new Runnable() {
@Override
public void run() {
tvLongitude.setText(context.getString(R.string.longitude) +": "+ preferences.getFloat("longitude", -1000));
tvLatitude.setText(context.getString(R.string.latitude) +": "+ preferences.getFloat("latitude", -1000));
tvSpeed.setText(context.getString(R.string.speed) +": "+ preferences.getFloat("speed", -1000));
tvAltitude.setText(context.getString(R.string.altitude) +": "+ preferences.getFloat("altitude", -1000));
tvAccuracy.setText(context.getString(R.string.accuracy) +": "+ preferences.getFloat("accuracy", -1000));
handler.postDelayed(this, 2000);
}
};
// Start the initial runnable task by posting through the handler
handler.post(runnableCode);
return view; return view;
} }

View File

@ -18,36 +18,36 @@
<TextView <TextView
android:id="@+id/tv_position" android:id="@+id/tv_position"
android:text="Position" android:text="@string/position"
android:padding="10dp" android:paddingVertical="10dp"
android:textSize="25sp" android:textSize="25sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
<TextView <TextView
android:id="@+id/tv_longitude" android:id="@+id/tv_longitude"
android:text="Breitengrag" android:text="@string/longitude"
android:textSize="25sp" android:textSize="25sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
<TextView <TextView
android:id="@+id/tv_latitude" android:id="@+id/tv_latitude"
android:text="Längengrad" android:text="@string/latitude"
android:textSize="25sp" android:textSize="25sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
<TextView <TextView
android:id="@+id/tv_speed" android:id="@+id/tv_speed"
android:text="Geschwindigkeit" android:text="@string/speed"
android:textSize="25sp" android:textSize="25sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
<TextView <TextView
android:id="@+id/tv_altitude" android:id="@+id/tv_altitude"
android:text="Seehöhe" android:text="@string/altitude"
android:textSize="25sp" android:textSize="25sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
@ -55,7 +55,7 @@
<TextView <TextView
android:id="@+id/tv_accuracy" android:id="@+id/tv_accuracy"
android:text="Genauigkeit" android:text="@string/accuracy"
android:textSize="25sp" android:textSize="25sp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />

View File

@ -15,6 +15,12 @@
<string name="google_maps_key">AIzaSyBQhcmDlyP01yNwTd4RTlatQIAJH3wJHw0</string> <string name="google_maps_key">AIzaSyBQhcmDlyP01yNwTd4RTlatQIAJH3wJHw0</string>
<string name="map">Karte</string> <string name="map">Karte</string>
<string name="satellite">Satelit</string> <string name="satellite">Satellit</string>
<string name="longitude">Längengrad</string>
<string name="latitude">Breitengrad</string>
<string name="speed">Geschwindigkeit</string>
<string name="altitude">Seehöhe</string>
<string name="accuracy">Genauigkeit</string>
<string name="position">Position:</string>
</resources> </resources>