等待用户许可继续

我想让我的应用程序在应用程序继续之前等待用户授予权限。我搜索了有关它的其他线程,发现常见的方法是使用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();

}

和请求结果:


@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) {

                progressBar.setVisibility(View.VISIBLE);

                GPSTracker gt = new GPSTracker(getActivity().getApplicationContext());

                Location location = gt.getLocation();




回首忆惘然
浏览 147回答 1
1回答

慕的地10843

就像评论中提到的 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 的问题中提到的一样:@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) {                //GPS method            } else {                // Toast for user            }            return;        }    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java