Android"gps需要ACCESS_FINE_LOCATION"错误 [英] Android “gps requires ACCESS_FINE_LOCATION” error

查看:211
本文介绍了Android"gps需要ACCESS_FINE_LOCATION"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是SDK-23,每次运行该应用程序时,都会抛出SecurityException,调试器中的错误如下:

I am using SDK-23, and every time I run the application, my SecurityException gets thrown and the error from the debugger reads as so:

java.lang.SecurityException:"gps"位置提供程序需要 ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限.

java.lang.SecurityException: "gps" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.

这似乎是一个简单的错误,但是我的清单文件是完全正确的.在这里,这也是我的MapActivity代码:

This seems like a simple mistake, however, my manifest file is completely correct. Here it is, and here is my MapActivity code as well:

package com.buahbatu.salesmonitoring;



public class MainActivity extends AppCompatActivity implements View.OnClickListener {
final static String TAG = "MainActivity";
SwitchCompat switchTracked;
MyService service;
private GoogleMap googleMap;
Context mContext;
private TextView textAddress;


/* Google Fused Location Service */
public static GoogleApiClient mGoogleApiClient;
public static LocationRequest mLocationRequest;
public static GoogleApiClient.ConnectionCallbacks connectionCallbacks;
public static GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener;
public final static int REQUEST_LOCATION = 199;
public final static int REQUEST_CONNECTION = 11;
public final static int NOTIFICATION_ID = 2;

private static final String[] INITIAL_PERMS={
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.READ_CONTACTS
};
private static final String[] LOCATION_PERMS={
        Manifest.permission.ACCESS_FINE_LOCATION
};


boolean checkPermission() {
    String location_fine = "android.permission.ACCESS_FINE_LOCATION";
    String location_coarse = "android.permission.ACCESS_COARSE_LOCATION";
    int permission_fine = mContext.checkCallingOrSelfPermission(location_fine);
    int permission_coarse = mContext.checkCallingOrSelfPermission(location_coarse);
    return permission_fine == PackageManager.PERMISSION_GRANTED && permission_coarse == PackageManager.PERMISSION_GRANTED;
}


public void startTracking(Activity activity) {
    if (checkPermission()) {
        Log.i(TAG, "startTracking");
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
    } else {
        int permissionCheck = ContextCompat.checkSelfPermission(activity,
                Manifest.permission.ACCESS_FINE_LOCATION);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    try {
        service.setUpdateView(null);
        initilizeMap();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    checkLoggedStatus();
    setSupportActionBar((Toolbar) findViewById(R.id.top_toolbar));
    ((TextView) findViewById(R.id.username_text)).setText(AppConfig.getUserName(this));
    switchTracked = (SwitchCompat) findViewById(R.id.tracked_switch);
    switchTracked.setOnCheckedChangeListener(onCheckedChangeListener);
    switchTracked.setChecked(AppConfig.getOnTracked(this));
    findViewById(R.id.test_but).setOnClickListener(this);
    textAddress = (TextView) findViewById(R.id.txtAddress);
}

CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!isChecked) {
            switchTracked.setText(R.string.untracked);
            service.stopTracking();
            AppConfig.saveOnTracked(MainActivity.this, false);
        } else {
            switchTracked.setText(R.string.tracked);
            service = AcaciaX.createService(getApplicationContext(), MyService.class);
            service.startTracking(MainActivity.this);
            service.setUpdateView((TextView) findViewById(R.id.location_text));
            AppConfig.saveOnTracked(MainActivity.this, true);
        }
    }
};

void checkLoggedStatus() {
    if (!AppConfig.getLoginStatus(this)) {
        moveToLogin();
    }
}

void moveToLogin() {
    Intent move = new Intent(this, LoginActivity.class);
    startActivity(move);
    finish();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(TAG, Integer.toString(resultCode));

    //final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
    switch (requestCode) {
        case ServiceImpl.REQUEST_CONNECTION:
            switch (resultCode) {
                case Activity.RESULT_OK: {

                    switchTracked.setChecked(true);
                    break;
                }
                case Activity.RESULT_CANCELED: {

                    Toast.makeText(this, "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show();
                    break;
                }
                default: {
                    break;
                }
            }
            break;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_logout) {
        AppConfig.saveLoginStatus(this, false);
        AppConfig.storeAccount(this, "", "");
        switchTracked.setChecked(false);

        moveToLogin();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    NetHelper.login(MainActivity.this, "Tester", "pasu", new PostWebTask.HttpConnectionEvent() {
        @Override
        public void preEvent() {

        }

        @Override
        public void postEvent(String... result) {

        }
    });

}

private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();
        setUpMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
        if (googleMap != null) {
            //setUpMap();
        }
    }

}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
    //googleMap.setMyLocationEnabled(true);
    setUpMap();
}

public void setUpMap() {
    if(checkPermission()) {
        googleMap.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);

        CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude()));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);

        googleMap.moveCamera(center);
        googleMap.animateCamera(zoom);

        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this, Locale.getDefault());

        try {
            addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

            if (addresses != null) {
                String address = addresses.get(0).getAddressLine(0);
                String city = addresses.get(0).getLocality();
                String state = addresses.get(0).getAdminArea();
                String country = addresses.get(0).getCountryName();
                String postalCode = addresses.get(0).getPostalCode();
                String knowName = addresses.get(0).getFeatureName();

                String addressfull = address + " " + city + " " + state + " " + country + " " + postalCode + " " + knowName;

                Intent intent = new Intent();
                intent.putExtra("addressfull", addressfull);
                textAddress.setText(addressfull);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }else{
        requestPermissions(INITIAL_PERMS, 2);
    }

}

}

推荐答案

如果您定位的是Android M,则需要向用户请求访问设备GPS的权限.这是一个不太干净的代码,但可能会有所帮助.更好的方法是创建您自己的权限管理器类以处理请求和alertDialog主动性.

If you are targeting Android M, you need to ask users permission to access device GPS. Here is a not so clean code, but might help u a little bit. A better way to do it would be to create your own permission manager class to handle requests and a alertDialog ativity.

下面发生了什么

1)您正在检查是否授予了权限. 2)如果不是,则您正在检查权限是否先前已被拒绝,在这种情况下,您将显示出要向用户解释为什么需要权限的基本原理. 3)使用ActivityCompat显示请求权限弹出窗口. 4)如果用户拒绝,只要您需要访问GPS但注意到未授予权限,就显示带有查看按钮的小吃店,将用户带到应用程序信息屏幕.

1) You are checking if permissions are granted. 2) If not, you are checking if the permissions have been denied previously, In that case, you are showing a rationale, to explain to the user why you need the permissions. 3) You show the request permission popup using ActivityCompat. 4) If user declines, show a snackbar with a view button to take the user to the app info screen, whenever u need to access the GPS but notice that permissions are not granted.

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_COARSE_LOCATION)) {
            showRationale();
        } else {
            // do request the permission
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 8);
        }
    }

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ){
                //Start your code
            } else {
                //Show snackbar
            }
    }
}

private void showRationale(){
    String strDeleteMessage = getResources().getString(R.string.rationale_message11) ;

    final View dialogView = LayoutInflater.from(this.getActivity()).inflate(R.layout.dialog_fragment, null);

    final AlertDialog storageRationaleAlert = new AlertDialog.Builder(this.getActivity()).create();
    storageRationaleAlert.setView(dialogView, 0, 0, 0, 0);
    storageRationaleAlert.setCanceledOnTouchOutside(false);
    TextView mDialogTitle = (TextView) dialogView.findViewById(R.id.dialog_title);
    TextView mDialogDetails = (TextView) dialogView.findViewById(R.id.dialog_details);
    mDialogDetails.setVisibility(View.VISIBLE);
    Button mCancelButton = (Button) dialogView.findViewById(R.id.cancel_btn);
    Button mOkButton = (Button) dialogView.findViewById(R.id.ok_btn);
    mOkButton.setText(getString(R.string.dialog_continue));

    mDialogDetails.setText(Html.fromHtml(strDeleteMessage));

    final Activity activity = this.getActivity();
    mOkButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            storageRationaleAlert.dismiss();

            //Ask for GPS permission
        }
    });

    mCancelButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            storageRationaleAlert.dismiss();
            //Show permission snackbar
        }
    });

    storageRationaleAlert.show();
}

这篇关于Android"gps需要ACCESS_FINE_LOCATION"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆