Can't turn on location on android api23?

后端 未结 2 601
旧巷少年郎
旧巷少年郎 2021-01-24 13:49

I have used the following code and the dialogue that asks for permission shows as expected. But when I click \"allow\" it doesn\'t do anything. The log message doesn\'t appear a

相关标签:
2条回答
  • 2021-01-24 14:15

    you might have to add a dependency in your build.gradle:

    compile 'com.google.android.gms:play-services-location:10.0.1

    0 讨论(0)
  • 2021-01-24 14:31

    try this code:

    private LocationCoord gps = null;
    private static final int PERMISSION_REQUEST_CODE = 1;
    

    In OnCreate():

    //GPS Manage
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        boolean gps_enabled = false;
        boolean network_enabled = false;
    
        try {
            gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
        }
    
        try {
            network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
        }
    
        if (!gps_enabled && !network_enabled) {
            // notify user
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setMessage("Allow ImHere to access this device's location?");
            dialog.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    // TODO Auto-generated method stub
                    Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(myIntent);
                    //get gps
                }
            });
            dialog.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    // TODO Auto-generated method stub
    
                }
            });
            dialog.show();
        }
    
        gps = new LocationCoord(this);
    

    @Override
    protected void onStart() {
        super.onStart();
    
        // permission android 6.0
        if (!checkPermission()) {
            requestPermission();
        }
    
    }
    
    
    private boolean checkPermission(){
        int result = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
        if (result == PackageManager.PERMISSION_GRANTED) return true;
        else return false;
    }
    
    private void requestPermission(){
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
    }
    

    You will need this permissions on the Manifest.xml:

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    

    You can get LoocationCord.java here: https://github.com/toomyy94/ImHere-Chatbot/blob/master/app/src/main/java/pt/ua/tomasr/imhere/modules/LocationCoord.java

    0 讨论(0)
提交回复
热议问题