位置权限 服务 开启

    private LocationManager lm;//【位置管理】

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        findViewById(R.id.checkBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check();
            }
        });
        findViewById(R.id.requestBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                request();
            }
        });
    }
    public void  request(){
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            // 没有权限,申请权限。
            Toast.makeText(this, "没有权限", Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},0);
            Toast.makeText(this,"请求权限",Toast.LENGTH_SHORT).show();
        } else {
            // 有权限了,去放肆吧。
            Toast.makeText(this, "有权限", Toast.LENGTH_SHORT).show();
        }
    }
    public void check(){
          lm = (LocationManager) this.getSystemService(this.LOCATION_SERVICE);
        boolean ok = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (ok) {//开了定位服务
            Toast.makeText(this,"已开启位置服务",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "系统检测到未开启GPS定位服务", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(intent, 1315);
        }

    }
原文地址:https://www.cnblogs.com/the-wang/p/10239049.html