我搜索,搜索和搜索,但无法找到解决问题的方法。
我有一个非常简单的位置,可以返回此人的当前地址。 当您在4.2.2设备中打开应用程序时,会出现通知栏上的GPS图标,当您按“我的位置”按钮时,将返回该地址。
但是当在4.0.3和4.0.4中运行应用程序时,通知栏上不显示GPS图标,当点击“我的位置”时,应用程序将关闭。
我是Android开发的新手,所以我不知道为什么会这样。
有没有人知道如何修复它?
感谢。
这是我的清单文件的一部分:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATIO" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
部分代码:
公共类MainActivity extends Activity实现ActivityConstants {
private static final long MIN_DIST_CHANGE_FOR_UPDATES = 1; //in meters
private static final long MIN_TIME_BETWEEN_UPDATES = 0; //in milliseconds
protected LocationManager locationManager;
private String provider;
Criteria criteria;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager=LocationManager)getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(provider, MIN_TIME_BETWEEN_UPDATES, MIN_DIST_CHANGE_FOR_UPDATES, new MyLocationListener());
}
public void myLocation(View view) {
Location location =locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// If location is not null show a Toast with the location.
if (location != null) {
String message = String.format("Current location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude());
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Latitude and longitude were not found", Toast.LENGTH_LONG).show();
}
if (Geocoder.isPresent()) {
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
List<Address> addresses = null;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
if (addresses.size() > 0) {
String address = "";
for (int i = 0; i < addresses.get(0).getMaxAddressLineIndex(); i++) {
address += addresses.get(0).getAddressLine(i) + "\n";
}
Toast.makeText(MainActivity.this, "Your current address is: \n" + address, Toast.LENGTH_LONG).show();
}
}
}