栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java如何在运行时请求位置许可

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java如何在运行时请求位置许可

实际上需要在运行时请求“位置”权限(请注意代码中的说明)。

这是经过测试的有效代码,用于请求“位置”权限。

确保导入

android.Manifest

import android.Manifest;

然后将此代码放在Activity中:

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;public boolean checkLocationPermission() {    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {        // Should we show an explanation?        if (ActivityCompat.shouldShowRequestPermissionRationale(this,     Manifest.permission.ACCESS_FINE_LOCATION)) { // Show an explanation to the user *asynchronously* -- don't block // this thread waiting for the user's response! After the user // sees the explanation, try again to request the permission. new alertDialog.Builder(this)         .setTitle(R.string.title_location_permission)         .setMessage(R.string.text_location_permission)         .setPositiveButton(R.string.ok, new DialogInterface.onClickListener() {  @Override  public void onClick(DialogInterface dialogInterface, int i) {      //prompt the user once explanation has been shown      ActivityCompat.requestPermissions(MainActivity.this,   new String[]{Manifest.permission.ACCESS_FINE_LOCATION},   MY_PERMISSIONS_REQUEST_LOCATION);  }         })         .create()         .show();        } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(this,         new String[]{Manifest.permission.ACCESS_FINE_LOCATION},         MY_PERMISSIONS_REQUEST_LOCATION);        }        return false;    } else {        return true;    }}@Overridepublic void onRequestPermissionsResult(int requestCode,      String permissions[], int[] grantResults) {    switch (requestCode) {        case MY_PERMISSIONS_REQUEST_LOCATION: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {     // permission was granted, yay! Do the     // location-related task you need to do.     if (ContextCompat.checkSelfPermission(this,  Manifest.permission.ACCESS_FINE_LOCATION)  == PackageManager.PERMISSION_GRANTED) {         //Request location updates:         locationManager.requestLocationUpdates(provider, 400, 1, this);     } } else {     // permission denied, boo! Disable the     // functionality that depends on this permission. } return;        }    }}

然后在中调用

checkLocationPermission()
方法
onCreate()

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //.........    checkLocationPermission();}

然后

onResume()
,您可以使用和
onPause()
完全按照问题中的说明进行操作。

这是一个精简版本,更加简洁:

@Overrideprotected void onResume() {    super.onResume();    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {        locationManager.requestLocationUpdates(provider, 400, 1, this);    }}@Overrideprotected void onPause() {    super.onPause();    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {        locationManager.removeUpdates(this);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/415880.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号