1

我搜索,搜索和搜索,但找不到解决问题的方法。

我有一个非常简单的位置,它返回该人的当前地址。当您在 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 扩展 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();

        }


    }
}
4

0 回答 0