新许可系统(位置整合)

时间:2016-06-10 10:37:14

标签: android android-studio location weather-api

这是我的第一个问题。我学习计算机科学,并试图提高我在android开发方面的技能。因此我想制作一个简单的天气应用程序。我不习惯android 6.0的新权限系统,所以我需要你的帮助。

我总是遇到问题,日志告诉我,找不到位置(如果位置为null,我使用的字符串)。我这样做,因为我想要检查是否有任何位置。之后我想将纬度和高度传递给API,以请求坐标的天气数据。请查看我的代码并尝试帮助我,并给我一些改进代码的建议:

public class MainActivity extends AppCompatActivity implements LocationListener {

LocationManager locationManager;
Location location;
String provider;
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //LocationManager Setup get System Services
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    provider = locationManager.getBestProvider(new Criteria(), false);

    //Get Last known Location
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, permissions, 10);

    } else {

        locationManager.requestLocationUpdates(provider, 400, 1000, this);
        location = locationManager.getLastKnownLocation(provider);

        if (location != null) {
            Log.i("Location Info", "Location Achieved");
        } else {
            Log.i("Location Info", "No Location found");
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, permissions, 10);
        return;
    } else {
        locationManager.requestLocationUpdates(provider, 400, 1000, this);
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, permissions, 10);
        return;
    } else {
        locationManager.removeUpdates(this);
    }
}



@Override
public void onLocationChanged(Location location) {
    Double lat = location.getLatitude();
    Double lng = location.getLongitude();

    Log.i("Latitude", lat.toString());
    Log.i("Longitude", lng.toString());
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
}

1 个答案:

答案 0 :(得分:0)

您需要处理Permissions Result

@Override
public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
switch (requestCode) {
    case MY_PERMISSIONS_REQUEST_LOCATION: { // for your case 10
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! Do the
            // location-related task you need to do.

        } else {

            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
        return;
    }

    // other 'case' lines to check for other
    // permissions this app might request
}
}