Android:定位服务权限

Android: Location Service permission

我从位置管理器访问位置时遇到问题

这是错误

04-24 11:12:10.999 9378-9378/com.example.devandrin.myapplication D/HomeActivity: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.

定位功能代码

LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean GPS;
    try {
        GPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception e) {
        GPS = false;
    }
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    boolean data = info != null;
    if (!data || !GPS) {
        Utilities.MakeSnack(findViewById(R.id.cLayout), "Unable to get location");
    } else {
        try {
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1200, 50, new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {

                }

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

                }

                @Override
                public void onProviderEnabled(String provider) {

                }

                @Override
                public void onProviderDisabled(String provider) {

                }
            });
            Location l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            String url = "/setuserlocation/" + sp.getString("userID", "") +
                    "/" + l.getLatitude() +
                    "/" + l.getLongitude();
            StringRequest sr = new StringRequest(StringRequest.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Utilities.MakeToast(getInstance(), "Location Updated");
                    GregorianCalendar gc = new GregorianCalendar();
                    Date date = gc.getTime();
                    SharedPreferences.Editor e = sp.edit();
                    e.putString("DatePosted", date.toString());
                    e.commit();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Utilities.MakeToast(getInstance(), "Location Wasn't updated");
                }
            });
            RequestQueueSingleton.getInstance(this).getRequestQueue().add(sr);

        } catch (SecurityException e) {
            Log.d("HomeActivity", e.getMessage());
        }

    }

以及清单中设置的权限

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

应用程序的 api 级别是 16,因此我无法在运行时请求权限,应用程序也不会在安装时请求权限,谁能帮我解决这个问题....

像这样获得您的 运行 时间许可

    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
                    PackageManager.PERMISSION_GRANTED &&
                    ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
                            PackageManager.PERMISSION_GRANTED) {
                    // Permission already Granted
                    //Do your work here
//Perform operations here only which requires permission
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
            }

如果尚未授予权限,则像这样重写 onRequestPermission 结果

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            if (requestCode == 1) {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                            //Permission Granted
                            //Do your work here
//Perform operations here only which requires permission
                }
            }
        }