【问题标题】:New Permission System (Integration of Location)新权限系统(位置整合)
【发布时间】:2016-10-11 07:04:47
【问题描述】:

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

我总是有问题,日志告诉我,没有找到位置(我使用的字符串,如果位置为空)。我正在这样做,因为我想检查是否有任何位置。之后我想将纬度和高度传递给 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) {

}
}

【问题讨论】:

    标签: android android-studio location weather-api


    【解决方案1】:

    你需要处理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
    }
    }
    

    【讨论】:

    • 谢谢很多朋友。这有帮助。
    猜你喜欢
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 2020-08-06
    • 2012-04-15
    • 2011-05-05
    • 2014-08-28
    相关资源
    最近更新 更多