Mobile_Application_Developm.../DailyHelper/app/src/main/java/at/fhj/swd/dailyhelper/ui/fragemnt/HomeFragment.java

69 lines
2.5 KiB
Java

package at.fhj.swd.dailyhelper.ui.fragemnt;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import at.fhj.swd.dailyhelper.R;
import at.fhj.swd.dailyhelper.service.LocationService;
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
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
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;
}
}