我已經尋找解決方案,並嘗試了我可以找到相關的,但仍然無法得到這個工作。代碼的特定部分未運行。我遇到的問題是,這一切都適用於我的舊NEC地形(4.0.1,API 15),但不適用於我的新黑莓Priv(6.0.1,API 23)。在新手機中,第一個錯誤日誌「Net enabled」顯示在logcat中,然後沒有其他錯誤日誌出現(我正在使用它們進行故障排除)。在舊手機中,一切正常,所有日誌都顯示出來。什麼阻止這段代碼運行?我添加了用於添加權限的代碼,我已在下面發佈。我也有我的清單如下:在Android 6.0.1中的位置返回0.0,但在4.0.1中工作
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission-sdk-23 android:name="android.permission.INTERNET" />
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION"/>
下面是的LocationManager聲明:
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
而這裏的代碼:
if (isNetworkEnabled) {
Log.e("Tag", "Net Enabled");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.e("Tag", "No permission");
}
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager == null) {
Log.e("Tag", "It's null.");
}
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.e("Tag", "NetLocMan OK");
if (location != null) {
Log.e("Tag", "NetLoc OK");
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.e("Tag", "" + latitude);
Log.e("Tag", "" + longitude);
}
}
}
if (isGPSEnabled) {
Log.e("Tag", "Location" + location);
if (location == null) {
Log.e("Tag", "GPSLoc finding");
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.e("Tag", "Location" + location);
if (locationManager != null) {
Log.e("Tag", "GPSLocMan OK");
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.e("Tag", "Location" + location);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.e("Tag", "" + latitude);
Log.e("Tag", "" + longitude);
}
}
}
}
這是我正在使用的權限代碼:
private static final int PERMS_REQUEST_CODE = 123;
if (!hasPermissions()){
requestPerms();
}
private boolean hasPermissions(){
int res = 0;
//string array of permissions,
String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
for (String perms : permissions){
res = checkCallingOrSelfPermission(perms);
if (!(res == PackageManager.PERMISSION_GRANTED)){
return false;
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
boolean allowed = true;
switch (requestCode){
case PERMS_REQUEST_CODE:
for (int res : grantResults){
// if user granted all permissions.
allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
}
break;
default:
// if user not granted permissions.
allowed = false;
break;
}
if (allowed){
//user granted all permissions we can perform our task.
}
else {
// we will give warning to user that they haven't granted permissions.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)){
Toast.makeText(this, "Permissions denied.\nCannot continue.", Toast.LENGTH_SHORT).show();
}
if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)){
Toast.makeText(this, "Permissions denied.\nCannot continue.", Toast.LENGTH_SHORT).show();
}
}
}
}
感謝您的幫助!如果您需要更多代碼,請讓我知道。
這是重複的。現在我知道了,我能做些什麼呢?刪除是否可接受? – swordguy8
嗯,我改變了一些問題,因爲權限不起作用(權限是在應用程序中授予的,但未運行的代碼仍然不會運行)。這在技術上還是重複的? – swordguy8