【无标题】

* 使用定位功能分两步:
 *    第一步:检查是否有定位权限,直接在当面Activity弹窗申请。
 *    第二步:检查是否开启了定位服务,如果没有开启,要跳到设置页面去设置。

package com.example.permissiontest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;


/**
 * 使用定位功能分两步:
 *    第一步:检查是否有定位权限,直接在当面Activity弹窗申请。
 *    第二步:检查是否开启了定位服务,如果没有开启,要跳到设置页面去设置。
 */
public class MainActivity extends Activity {

    private TextView tv_hello;


    private final int LOCATION_REQUEST_PERMISSION = 1001;
    private final int LOCATION_SERVICE_SETTINGS = 1002;

    private String[] permission_location = new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION/*, android.Manifest.permission.ACCESS_COARSE_LOCATION*/};

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

        tv_hello = findViewById(R.id.tv_hello);

        requestPermission(permission_location);
    }


    //第一步:申请权限
    public void requestPermission(String[] permission) {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.e("xxx", "申请权限");
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_PERMISSION);
        } else {
            nextStep();
        }
    }

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

        if (requestCode == LOCATION_REQUEST_PERMISSION) {
            //用户点击了“确定” == grantResults[0] == PackageManager.PERMISSION_GRANTED)
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Log.e("xxx","获取到了权限");

                 nextStep();
            } else {
               Toast.makeText(MainActivity.this,"警告:没有定位权限,有些功能可能不能使用",Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void nextStep(){
        if(isLocServiceEnable(this)){
            tv_hello.setText("恭喜你,现在可以使用定位服务了.");
        }else{
            Log.e("xxx","去设置定位服务");
            showSettingDialog();
        }
    }


    // 第二步:在确定我们的app本身已经具有了定位权限后,检查手机是否已经打开了定位服务,如果没有开启,去设置
    public boolean isLocServiceEnable(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (gps || network) {
            return true;
        }

        return false;
    }

    public void showSettingDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage("尚未开启位置定位服务");

        builder.setPositiveButton("开启", new DialogInterface.OnClickListener() {
            @Override

            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, LOCATION_SERVICE_SETTINGS);
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.e("xxx","从设置页面回来了");

        if(requestCode == LOCATION_SERVICE_SETTINGS){
            //如果从设置页面回来了,再走头再走一次逻辑
            requestPermission(permission_location);
        }
    }
}



    

你可能感兴趣的:(动态申请定位权限)