在清单中添加了权限但访问被拒绝

超越开发解决方案

我创建了类 GPSTracker.java 以对我当前的 LatLng 表示敬意。在清单中设置我的权限,但 isNetworkEnabled 在 GPSTracker.java 中仍然显示红色并抛出 SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION 权限。谁能告诉我如何解决这个问题。我已经尝试了我能想到的一切。这是 GPSTracker.java 的代码:

public class GPSTracker extends Service implements LocationListener{

private final Context context;

boolean isGPSEnabled = false;
boolean canGetLocation = false;


Location location;
double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.context = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if(!isGPSEnabled && !isNetworkEnabled) {

        }
        else {
            this.canGetLocation = true;
            if(isNetworkEnabled) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            }
            if(locationManager !=null) {
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if(location != null) {
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                }
            }
        }
        if(isGPSEnabled) {
            if(location == null) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if(locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if(location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    return location;
}

public void stopUsingGPS() {
    if(locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

public double getLatitude() {
    if(location != null) {
        latitude = location.getLatitude();
    }
    return latitude;
}
public double getLongitude() {
    if(location != null) {
        longitude = location.getLongitude();
    }
    return longitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}

public void  showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle("GPS is settings");
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivity(intent);
        }
    });
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    alertDialog.show();
}

@Override
public void onLocationChanged(Location arg0) {

}
@Override
public void onProviderDisabled(String arg0) {

}
@Override
public void onProviderEnabled(String arg0) {

}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

}
@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

这是 NewCatch.java 上按钮的代码

btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {
                sqLiteHelper.insertData(

                        edtSpecies.getText().toString().trim(),
                        edtDate.getText().toString().trim(),
                        edtWeight.getText().toString().trim(),
                        edtLength.getText().toString().trim(),
                        edtSex.getText().toString().trim(),
                        edtBait.getText().toString().trim(),
                        edtMethod.getText().toString().trim(),
                        imageViewToByte(imageView)
                );
                Toast.makeText(getApplicationContext(), "Entry Added",     Toast.LENGTH_LONG).show();
                edtSpecies.setText("");
                edtDate.setText("");
                edtWeight.setText("");
                edtLength.setText("");
                edtSex.setText("");
                edtBait.setText("");
                edtMethod.setText("");
                imageView.setImageResource(R.drawable.fishing);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            //////
            gps = new GPSTracker(NewCatch.this);
            if(gps.canGetLocation()) {
                double latitude = gps.getLatitude();
                double longitude = gps.getLongitude();
                Toast.makeText(getApplicationContext(), "Location Saved     -\nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG);
            }
            else {
                gps.showSettingsAlert();
            }
            //////
        }
    });

最后但并非最不重要的是我的清单:

    <?xml version="1.0" encoding="utf-8"?>
<!--suppress AndroidDomInspection -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="devsolutionsbeyond.media.fishinglog">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Dash">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".NewCatch" />
    <activity android:name=".About" />
    <activity android:name=".FishList" />
    <activity android:name=".Maps"
        android:label="@string/title_activity_maps" />



    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.android.Fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
            <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />
    <service android:name=".GPSTracker" />
</application>

苦山

你检查权限,如果没有找到,请求一个,像这样:

        if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){

            ActivityCompat.requestPermissions(LoginActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1600);

        }

您可以得到如下结果:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],@NonNull int[] grantResults) {
    if(requestCode==REQUESTID){
        // If request is cancelled, the result arrays are empty.
        if (!(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {

            // permission was not granted, boo!
            //show the permission rationale if the user did not click never show again

            if(ActivityCompat.shouldShowRequestPermissionRationale(LoginActivity.this,android.Manifest.permission.ACCESS_FINE_LOCATION)){
                //means user did not choose to never show permissions again. show the alert dialog
                //Show a message explaining why the permission is necessary
                //This explanation will only be shown if the user hasn't clicked do not show again on the permission dialog
            }

        }
    }
}

和往常一样,将您的位置请求调用放在 try catch 块中,因为用户总是可以拒绝会导致崩溃的权限

                try {

                    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

                } catch (SecurityException se) {
                    Toast.makeText(this, "Please provide location permissions to continue", Toast.LENGTH_SHORT).show();
                }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

java.lang.SecurityException:权限被拒绝(缺少INTERNET权限?),即使在Android清单中添加了此权限

来自分类Dev

如果在 docker build 期间添加了该目录,如何修复拒绝访问目录的权限?

来自分类Dev

清单中添加了android.permission.PHONE_CALL,但仍然出现权限错误

来自分类Dev

清单中添加了android.permission.PHONE_CALL,但仍然出现权限错误

来自分类Dev

尽管在代理中添加了身份,但仍无法使用SourceTree推送到github:权限被拒绝(公钥)

来自分类Dev

getSelectedDataAsync方法清单中ReadWriteDocument级别上的权限被拒绝

来自分类Dev

即使在清单中声明了ReadExternalStorage权限也被拒绝

来自分类Dev

在Powershell中添加邮箱,权限被拒绝

来自分类Dev

错误:拒绝访问钓鱼者2中的属性“拒绝”的权限

来自分类Dev

在我的本地仓库中添加了第二个Heroku应用程序,我被拒绝访问我的应用程序

来自分类Dev

JavaScript添加了C#中不可访问的行

来自分类Dev

尝试在Android中访问联系人时拒绝权限

来自分类Dev

iFrame中的SelectionContext-拒绝访问属性“ getSelection”的权限

来自分类Dev

在访问权限中添加其他标签

来自分类Dev

Chrome扩展程序被拒绝,清单文件不正确。权限中的URL

来自分类Dev

即使在Android清单中声明了Android联系人权限也被拒绝

来自分类Dev

拒绝访问属性“ CKEDITOR”的权限

来自分类Dev

错误:拒绝访问属性“ $”的权限

来自分类Dev

拒绝访问属性“文档”的权限

来自分类Dev

权限通过SSH访问被拒绝

来自分类Dev

错误:拒绝访问属性“ $”的权限

来自分类Dev

权限通过SSH访问被拒绝

来自分类Dev

(13)权限被拒绝:拒绝访问/〜me

来自分类Dev

错误= 13,在CodenameOne中添加事件时,权限被拒绝

来自分类Dev

添加文档访问被拒绝

来自分类Dev

终端中的权限被拒绝

来自分类Dev

权限被拒绝,但已添加组

来自分类Dev

无法加载Webview(添加了权限)

来自分类Dev

Android Studio中的清单权限

Related 相关文章

  1. 1

    java.lang.SecurityException:权限被拒绝(缺少INTERNET权限?),即使在Android清单中添加了此权限

  2. 2

    如果在 docker build 期间添加了该目录,如何修复拒绝访问目录的权限?

  3. 3

    清单中添加了android.permission.PHONE_CALL,但仍然出现权限错误

  4. 4

    清单中添加了android.permission.PHONE_CALL,但仍然出现权限错误

  5. 5

    尽管在代理中添加了身份,但仍无法使用SourceTree推送到github:权限被拒绝(公钥)

  6. 6

    getSelectedDataAsync方法清单中ReadWriteDocument级别上的权限被拒绝

  7. 7

    即使在清单中声明了ReadExternalStorage权限也被拒绝

  8. 8

    在Powershell中添加邮箱,权限被拒绝

  9. 9

    错误:拒绝访问钓鱼者2中的属性“拒绝”的权限

  10. 10

    在我的本地仓库中添加了第二个Heroku应用程序,我被拒绝访问我的应用程序

  11. 11

    JavaScript添加了C#中不可访问的行

  12. 12

    尝试在Android中访问联系人时拒绝权限

  13. 13

    iFrame中的SelectionContext-拒绝访问属性“ getSelection”的权限

  14. 14

    在访问权限中添加其他标签

  15. 15

    Chrome扩展程序被拒绝,清单文件不正确。权限中的URL

  16. 16

    即使在Android清单中声明了Android联系人权限也被拒绝

  17. 17

    拒绝访问属性“ CKEDITOR”的权限

  18. 18

    错误:拒绝访问属性“ $”的权限

  19. 19

    拒绝访问属性“文档”的权限

  20. 20

    权限通过SSH访问被拒绝

  21. 21

    错误:拒绝访问属性“ $”的权限

  22. 22

    权限通过SSH访问被拒绝

  23. 23

    (13)权限被拒绝:拒绝访问/〜me

  24. 24

    错误= 13,在CodenameOne中添加事件时,权限被拒绝

  25. 25

    添加文档访问被拒绝

  26. 26

    终端中的权限被拒绝

  27. 27

    权限被拒绝,但已添加组

  28. 28

    无法加载Webview(添加了权限)

  29. 29

    Android Studio中的清单权限

热门标签

归档