为了账号安全,请及时绑定邮箱和手机立即绑定

等待用户许可继续

等待用户许可继续

回首忆惘然 2021-06-28 17:58:57
我想让我的应用程序在应用程序继续之前等待用户授予权限。我搜索了有关它的其他线程,发现常见的方法是使用onRequestPermissionResult. 现在我对如何设置它一无所知:我是将我的程序代码放入onRequestPermissionResult除了请求本身还是我需要如何设置它,以便用户被要求然后应用程序继续?这是我的代码:if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {   if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)           != PackageManager.PERMISSION_GRANTED) {       ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);   } else {       progressBar.setVisibility(View.VISIBLE);       GPSTracker gt = new GPSTracker(getActivity().getApplicationContext());       Location location = gt.getLocation();       if (location == null) {           Toast.makeText(getActivity(), "No location found.",                   Toast.LENGTH_LONG).show();       } else {           lat = location.getLatitude();           lon = location.getLongitude();       }       new GetContacts().execute();   }} else {  progressBar.setVisibility(View.VISIBLE);  GPSTracker gt = new GPSTracker(getActivity().getApplicationContext());  Location location = gt.getLocation();  if (location == null) {     Toast.makeText(getActivity(), "No location found.",              Toast.LENGTH_LONG).show();  } else {      lat = location.getLatitude();      lon = location.getLongitude();  }    new GetContacts().execute();}和请求结果:@Overridepublic void onRequestPermissionsResult(int requestCode,                                       String permissions[], int[] grantResults) {    switch (requestCode) {        case 123: {            // If request is cancelled, the result arrays are empty            if (grantResults.length > 0                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                progressBar.setVisibility(View.VISIBLE);                GPSTracker gt = new GPSTracker(getActivity().getApplicationContext());                Location location = gt.getLocation();
查看完整描述

1 回答

?
慕的地10843

TA贡献1785条经验 获得超8个赞

就像评论中提到的 Martin Zeitler 一样,您必须为 Marshmallow 上方和下方的 API 做好准备,例如:


if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {


    if(ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)

     != PackageManager.PERMISSION_GRANTED) {


        ActivityCompat.requestPermissions(getActivity(), new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 123);

    } else {


        //GPS method

    }

} else {


    //GPS method

}

就像你在 onRequestResult 的问题中提到的一样:


@Override

public void onRequestPermissionsResult(int requestCode,

                                       String permissions[], int[] grantResults) {

    switch (requestCode) {

        case 123: {

            // If request is cancelled, the result arrays are empty

            if (grantResults.length > 0

                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {


                //GPS method

            } else {

                // Toast for user

            }

            return;

        }

    }

}


查看完整回答
反对 回复 2021-06-30
  • 1 回答
  • 0 关注
  • 146 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信