Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

android - cannot fetch location in marshmallow after giving permission request

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 and latitude is not fetched,if i go to settings and change the location from high accuracy to battery saving,location is fetched and the app works.I want location to be fetched at high accuracy.

                   ActivityCompat.requestPermissions(this,
            new String[]{
                    Manifest.permission.CAMERA,
                    android.Manifest.permission.ACCESS_FINE_LOCATION,
                    android.Manifest.permission.ACCESS_COARSE_LOCATION,
                    android.Manifest.permission.ACCESS_WIFI_STATE,
                    Manifest.permission.INTERNET,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.WAKE_LOCK,
            },
            1);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this

step 1 :- add this permission in manifiesr file

 android.Manifest.permission.ACCESS_FINE_LOCATION,
 android.Manifest.permission.ACCESS_COARSE_LOCATION,

step 2 : ask runtime permission

 String permission = android.Manifest.permission.ACCESS_FINE_LOCATION;
            if (ActivityCompat.checkSelfPermission(SearchCityClass.this, permission)
                    != PackageManager.PERMISSION_GRANTED && ActivityCompat.
                    checkSelfPermission(SearchCityClass.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(SearchCityClass.this, new String[]
                        {permission}, PERMISSION_GPS_CODE);

            }

step 3: handle permsiion result

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == PERMISSION_GPS_CODE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {


            Toast.makeText(this, location_permission_granted_msg, Toast.LENGTH_SHORT).show();

        } else {

            Toast.makeText(this, location_permission_not_granted_msg, Toast.LENGTH_SHORT).show();
        }
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...