cannot fetch location in marshmallow after giving permission request

后端 未结 3 1909
长发绾君心
长发绾君心 2020-12-12 02:40

I am developing an location app which is working fine in all devices except in Marshmallow.I have requested permission during runtime and when I grant permission longitude a

3条回答
  •  离开以前
    2020-12-12 03:25

    Add this code your Activity class

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_images);
    
        requestLocationPermission();
        //your other codes
    }
    
    private void requestLocationPermission() {
    
    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) {
    
            } else {
                ActivityCompat.requestPermissions(this, new String[]
       {
        Manifest.permission.ACCESS_FINE_LOCATION, 
        Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
        }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    
        //Checking the request code of our request
         //If permission is granted
        if (requestCode == 1) {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //Displaying a toast
                Toast.makeText(this, "Permission granted now you can get the location", Toast.LENGTH_LONG).show();
    
                } else {
                //Displaying another toast if permission is not granted
                Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
               }
    }
    

    Add this on your Mainfest XML

        
        
    

提交回复
热议问题