我正在构建一个应用程序,在屏幕上的某些更新信息之后。我已经添加了xML和Java的代码,如果有人可以告诉我id文本视图和Java代码以粗体显示,我将不胜感激。
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:padding="0dp">
<ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/imageView" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:src="@drawable/background" android:scaleType="centerCrop" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hiker's Watch" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30sp" android:textColor="#f3202020" android:layout_marginTop="30dp" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Latitude: " android:id="@+id/lat" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:textSize="20sp" android:textColor="#151515" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Longitude:" android:id="@+id/lng" android:layout_below="@+id/lat" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:textSize="20sp" android:textColor="#151515" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Accuracy: 20.0m" android:id="@+id/accuracy" android:layout_below="@+id/lng" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:textSize="20sp" android:textColor="#151515" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Speed:" android:id="@+id/speed" android:layout_below="@+id/accuracy" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:textSize="20sp" android:textColor="#151515" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bearing: " android:id="@+id/bearing" android:layout_below="@+id/speed" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:textSize="20sp" android:textColor="#151515" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Altitude:" android:id="@+id/altitude" android:layout_below="@+id/bearing" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:textSize="20sp" android:textColor="#151515" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Address: \n" android:id="@+id/address" android:layout_below="@+id/altitude" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:textSize="20sp" android:textColor="#151515" android:gravity="center_horizontal" />
</RelativeLayout>
Java
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends Activity implements LocationListener {
LocationManager locationManager;
String provider;
TextView latTV;
TextView lngTV;
TextView accuracyTV;
TextView speedTV;
TextView bearingTV;
TextView altitudeTV;
TextView addressTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latTV = (TextView) findViewById(R.id.lat);
lngTV = (TextView) findViewById(R.id.lng);
accuracyTV = (TextView) findViewById(R.id.accuracy);
speedTV = (TextView) findViewById(R.id.speed);
bearingTV = (TextView) findViewById(R.id.bearing);
altitudeTV = (TextView) findViewById(R.id.altitude);
addressTV = (TextView) findViewById(R.id.address);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
@Override
public void onLocationChanged(Location location) {
Double lat = location.getLatitude();
Double lng = location.getLongitude();
Double alt = location.getAltitude();
Float bearing = location.getBearing();
Float speed = location.getSpeed();
Float accuracy = location.getAccuracy();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(lat, lng, 1);
if (listAddresses != null && listAddresses.size() > 0 ) {
Log.i("PlaceInfo", listAddresses.get(0).toString());
String addressHolder = "";
for (int i = 0; i <= listAddresses.get(0).getMaxAddressLineIndex(); i++) {
addressHolder += listAddresses.get(0).getAddressLine(i) + "\n";
}
addressTV.setText("Address:\n" + addressHolder);
}
} catch (IOException e) {
e.printStackTrace();
}
latTV.setText("Latitude: " + lat.toString());
lngTV.setText("Longitude: " + lng.toString());
altitudeTV.setText("Altitude: " + alt.toString() + "m");
bearingTV.setText("Bearing: " + bearing.toString());
speedTV.setText("Speed: " + speed.toString() + "m/s");
accuracyTV.setText("Accuracy: " + accuracy.toString() + "m");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
答案 0 :(得分:0)
String
内Double
和Float
.setText();
解析不正确。
修正:
latTV.setText("Latitude: " + String.valueOf(lat));
lngTV.setText("Longitude: " + String.valueOf(lng));
altitudeTV.setText("Altitude: " + String.valueOf(alt) + "m");
bearingTV.setText("Bearing: " + Float.toString(bearing));
speedTV.setText("Speed: " + Float.toString(speed) + "m/s");
accuracyTV.setText("Accuracy: " + Float.toString(accuracy) + "m");
答案 1 :(得分:0)
你应该使用Fused location api
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
TextView txtOutputLat, txtOutputLon;
Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
String lat, lon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtOutputLat = (TextView) findViewById(R.id.textView);
txtOutputLon = (TextView) findViewById(R.id.textView2);
buildGoogleApiClient();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(100); // Update location every second
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
lat = String.valueOf(mLastLocation.getLatitude());
lon = String.valueOf(mLastLocation.getLongitude());
}
updateUI();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
lat = String.valueOf(location.getLatitude());
lon = String.valueOf(location.getLongitude());
//get other relevant data from location like bearing,address
updateUI();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
buildGoogleApiClient();
}
synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onDestroy() {
super.onDestroy();
mGoogleApiClient.disconnect();
}
void updateUI() {
txtOutputLat.setText(lat);
txtOutputLon.setText(lon);
}
}
以下是一个详细示例:http://www.androidwarriors.com/2015/10/fused-location-provider-in-android.html
关于Fused location provider API
的最好的事情是,您无需担心location
更新,它会在一段时间后或之后为您提供最新且准确的location
和更新location
location
改变。
关于Fused location provider API
的另一件事是你不需要考虑最好的location
提供商,因为它会自动选择最适合你Android设备硬件的提供商。