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

时间:2017-12-22 22:28:17

标签: java android

我创建了类GPSTracker.java以便为我当前的LatLng干杯。在清单中设置我的权限,但isNetworkEnabled仍在GPSTracker.java中显示红色并抛出SecurityException:“network”位置提供程序需要ACCESS_COARSE_LOCATION或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>

3 个答案:

答案 0 :(得分:1)

在更高的Android版本(Android 6.0(API级别23)或更高版本)中,某些权限被标记为危险!

虽然您可以在Manifest中提及它们,但对于使用较旧Android版本的用户来说它已经足够了,但您仍然需要在运行时询问用户(For Higher Versions)!系统将使用对话框询问用户。 “(您的应用程序名称)想要访问您的位置”并且用户将选择允许或不允许,您将处理逻辑。

您可以看到Android Documentation

答案 1 :(得分:1)

您检查权限,如果没有找到,请求一个,如下所示:

button

您可以按如下方式获得结果:

document.getElementById("b").onclick = function() {
    var x = document.getElementById("x").value;
    var y = document.getElementById("y").value;
    var z = document.getElementById("z").value;
    var a = document.getElementById("a").value;
    var b = document.getElementById("g").value;
    var c = document.getElementById("c").value;
    var risultato = parseFloat(x) + parseFloat(y) + parseFloat(z) + parseFloat(a) + parseFloat(b) + parseFloat(c);

    document.getElementById("risultato").innerHTML = "La massa del prodotto è " + risultato;
  }

与往常一样,将您的位置请求调用包含在try catch块中,因为用户始终可以拒绝将导致崩溃的权限

<form>
<input type="number" placeholder="Valore 1" id="x" required>

<input type="number" placeholder="Valore 2" id="y" required>

<input type="number" placeholder="Valore 3 (se presente)" id="z">

<input type="number" placeholder="Valore 4 (se presente)" id="a">

<input type="number" placeholder="Valore 5 (se presente)" id="g">

<input type="number" placeholder="Valore 6 (se presente)" id="c">

<input type="button" class="btn btn-primary" id="b" value="Applic" />
</form>

<div id="risultato">

</div>

答案 2 :(得分:0)

在服务

内的代码中添加此行
boolean isNetworkEnabled= false;

并为安全性异常相关问题添加所需权限。