我一直在尝试使用 GPS 坐标,但它比我想象的要困难得多。经过几个小时的反复试验,我设法为我的模拟器输出了纬度和经度(模拟)。以下是我做过的两种方法:
第一种方式:
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
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.widget.TextView;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String text = "";
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null) {
showMyAddress(location);
}
final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
showMyAddress(location);
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 10, locationListener);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Log.i(TAG, "longitude: " + longitude);
Log.i(TAG, "latitude: " + latitude);
text = "longitude: " + longitude + ", " + "latitude: " + latitude;
TextView textView = (TextView) findViewById(R.id.txtvwMain);
textView.setText(text);
}
private void showMyAddress(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> myList;
try {
myList = myLocation.getFromLocation(latitude, longitude, 1);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
第二种方式:
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity implements LocationListener
{
private LocationManager lm;
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.txtvwMain);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);
Location location = lm.getLastKnownLocation(lm.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
tv.setText("latitude="+latitude+", longitude="+longitude);
}
@Override
public void onLocationChanged(Location arg0) {
String lat = String.valueOf(arg0.getLatitude());
String lon = String.valueOf(arg0.getLongitude());
Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
tv.setText("lat="+lat+", lon="+lon);
}
public void onProviderDisabled(String arg0) {
Log.e("GPS", "provider disabled " + arg0);
}
public void onProviderEnabled(String arg0) {
Log.e("GPS", "provider enabled " + arg0);
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
Log.e("GPS", "status changed to " + arg0 + " [" + arg1 + "]");
}
}
上述两种方式都可以将模拟纬度和经度打印到模拟器中的 TextView 上(运行 Android 4.2.2)。但是,当我将 .apk 文件上传到我的设备(运行 Android 4.0.4 的平板电脑)时,它就崩溃了。我不知道出了什么问题,因为我看不到任何错误消息。问题的根源是什么,我该如何解决?谢谢。