位置权限 android

In this tutorial, we’ll be discussing and implementing the new location permissions model in our android application.

在本教程中,我们将在android应用程序中讨论和实现新的位置权限模型。

Note: Google has taken a detour from the Android Alphabetical versions. Android Q has been renamed to Android 10. Since this tutorial was written before Google decided to do this, you’ll see Android Q at some places in the article.
注意:Google已绕过Android按字母顺序排列的版本。 Android Q已重命名为Android10。由于本教程是在Google决定执行此操作之前编写的,因此您会在本文的某些地方看到AndroidQ。

Android 10位置权限 (Android 10 Location Permissions)

With the introduction of Android 10, besides the dialog UI, the way of handling location permissions has also changed.
Now the user is allowed to choose whether they want location updates when the app is in the background.
For that a new permission needs to be declared in the Manifest file:

随着Android 10的引入,除了对话框UI之外,处理位置权限的方式也发生了变化。
现在,允许用户选择在后台运行应用程序时是否要更新位置。
为此,需要在清单文件中声明新的权限:

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

Calling this along with COARSE_LOCATION would pop up a dialog with three options:

与COARSE_LOCATION一起调用将弹出一个带有三个选项的对话框:

  • Always Allow总是允许
  • Allow only while using the app仅在使用应用程序时允许
  • Deny拒绝

On selecting Deny, the next time the dialog will show a fourth option – Deny & Do Not Ask Again.

在选择“拒绝”时,下一次对话框将显示第四个选项-“拒绝并不再询问”。

Always Allow ensures that you can poll for location updates in foreground and background.

始终允许确保您可以轮询前景和后台的位置更新。

If you select “Allow only while using the app”, the next time the permission dialog will only ask you to always allow the location permission or deny.

如果您选择“仅在使用应用程序时允许”,则下次权限对话框将仅要求您始终允许位置权限或拒绝。

In the following section, we’ll be implementing our first Android Q Application.
Let’s update the SDK Manager and also create a new AVD with Android Q.

在下一节中,我们将实现我们的第一个Android Q应用程序。
让我们更新SDK Manager,并使用Android Q创建新的AVD。

项目结构 (Project Structure)

Our build.gradle file is given below:

我们的build.gradle文件如下:

apply plugin: 'com.android.application'android {compileSdkVersion 'android-Q'defaultConfig {applicationId "com.journaldev.androidqlocationpermissions"minSdkVersion 16targetSdkVersion 'Q'versionCode 1versionName "1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}
}dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'implementation 'com.android.support.constraint:constraint-layout:1.1.3'testImplementation 'junit:junit:4.12'androidTestImplementation 'com.android.support.test:runner:1.0.2'androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

We’ve upgraded the dependencies for Android – Q.

我们已经升级了Android – Q的依赖项。

码 (Code)

The code for the activity_main.xml layout is given below:

下面给出了activity_main.xml布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"xmlns:app="https://schemas.android.com/apk/res-auto"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Buttonandroid:id="@+id/btnPermissions"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="LOCATION PERMISSION"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>

Inside the MainActivity.java :

在MainActivity.java内部:

package com.journaldev.androidqlocationpermissions;import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;public class MainActivity extends AppCompatActivity {Button btnPermissions;public static final int REQUEST_CODE_PERMISSIONS = 101;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnPermissions = findViewById(R.id.btnPermissions);btnPermissions.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {requestLocationPermission();}});}private void requestLocationPermission() {boolean foreground = ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;if (foreground) {boolean background = ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED;if (background) {handleLocationUpdates();} else {ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, REQUEST_CODE_PERMISSIONS);}} else {ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_BACKGROUND_LOCATION}, REQUEST_CODE_PERMISSIONS);}}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);if (requestCode == REQUEST_CODE_PERMISSIONS) {boolean foreground = false, background = false;for (int i = 0; i < permissions.length; i++) {if (permissions[i].equalsIgnoreCase(Manifest.permission.ACCESS_COARSE_LOCATION)) {//foreground permission allowedif (grantResults[i] >= 0) {foreground = true;Toast.makeText(getApplicationContext(), "Foreground location permission allowed", Toast.LENGTH_SHORT).show();continue;} else {Toast.makeText(getApplicationContext(), "Location Permission denied", Toast.LENGTH_SHORT).show();break;}}if (permissions[i].equalsIgnoreCase(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {if (grantResults[i] >= 0) {foreground = true;background = true;Toast.makeText(getApplicationContext(), "Background location location permission allowed", Toast.LENGTH_SHORT).show();} else {Toast.makeText(getApplicationContext(), "Background location location permission denied", Toast.LENGTH_SHORT).show();}}}if (foreground) {if (background) {handleLocationUpdates();} else {handleForegroundLocationUpdates();}}}}private void handleLocationUpdates() {//foreground and backgroundToast.makeText(getApplicationContext(),"Start Foreground and Background Location Updates",Toast.LENGTH_SHORT).show();}private void handleForegroundLocationUpdates() {//handleForeground Location UpdatesToast.makeText(getApplicationContext(),"Start foreground location updates",Toast.LENGTH_SHORT).show();}
}

In the onRequestPermissionResult method, we check whether the permission is granted or not.
If the foreground location permission is not granted then we break out of the loop.
This is because, without the foreground permission allowed, the background location permission is of no use.

onRequestPermissionResult方法中,我们检查是否授予了权限。
如果未授予前台位置许可,则我们将退出循环。
这是因为,在没有前台许可的情况下,后台位置许可是没有用的。

Once the permission is granted, you can poll for the location updates.

授予权限后,您可以轮询位置更新。

The output of the above application in action is given below:

上面应用程序的输出如下:

Android Q Location Permissions Output 1

Android Q位置权限输出1

We can go to the Settings | Apps | Permissions to view the permission is granted or not as shown in the illustration below:

我们可以转到设置| 应用程式| 权限 ,以查看权限被授予或不如下面的图:

Android Q Location Permissions Settings

Android Q位置权限设置

Once the permission is denied and do not ask again is selected, the dialog would no longer open.
一旦权限被拒绝并且不再询问,选择该对话框将不再打开。

That brings an end to this tutorial. You can download the complete Android Q Location Permissions Project from the link given below or check out the Github Repository for the full source code.

这样就结束了本教程。 您可以从下面给出的链接下载完整的Android Q位置权限项目 ,也可以查看Github存储库以获取完整的源代码。

AndroidQLocationPermissionsAndroidQLocationPermissions
Github Project LinkGithub项目链接

翻译自: https://www.journaldev.com/28028/android-10-location-permissions

位置权限 android

位置权限 android_Android 10 –位置权限相关推荐

  1. 确定NTFS权限应用的位置

    1.1.1 确定应用权限的位置 当您在父文件夹上设置权限后,在该文件夹中创建的新文件和子文件夹将继承这些权限.如果您不想他们继承权限,则在设置父文件夹的特殊权限时选择"应用于"框中 ...

  2. 解决Windows7修改hosts时提示:您没有权限在此位置中保存文件

    在Windows7系统中在,未做任何修改的情况下,修改了 hosts 文件并保存,会出现一个对话框:"警告提示为:你没有权限在此位置中保存文件.请与管理员联系以获得相应权限."  ...

  3. QQ另存为出现“你没有权限在此位置中保存文件,请与管理员联系以获得相应权限”

    不知为何的,莫名其妙的,突然的,无中生有的,在QQ群文件里面另存为的指定路径的文件夹出现"**你没有权限在此位置中保存文件,请与管理员联系以获得相应权限**"*下附解决步骤(满满的 ...

  4. 没有权限访问储存此文件的计算机,win10你没有权限在此位置中保存文件的解决方法...

    最近有朋友问小编win10系统提示你没有权限在此位置中保存文件怎么办,相信很多人都遇到过这种情况,有时我们在把文件保存到c盘中时会遇到错误提示:"你没有权限在此位置中保存文件.请与管理员联系 ...

  5. C:\Windows\System32\drivers\etc\hosts你没有权限在此位置位置中保存文件 解决方法集中 解决hosts

    C:\Windows\System32\drivers\etc\hosts你没有权限在此位置位置中保存文件 解决方法集中 (一)首先你要处理最容易忽略的问题 我发现很多人在处理hosts的时候权限会开 ...

  6. 电脑提示:你没有权限在此位置中保存文件

    最近在学习微服务中的eureka,想要自己在本地做一个域名映射,在网上查阅资料可知,修改C:\Windows\System32\drivers\etc目录下的hosts文件即可实现域名映射. 但是修改 ...

  7. Win10你没有权限在此位置中保存文件,请与管理员联系以获得相应权限

    问题背景: 保存文件时,弹窗:你没有权限在此位置中保存文件,请与管理员联系以获得相应权限. 解决方案: 1.右键需要保存的文件,点击"属性" 2.进入安全Tab 3.选择当前用户名 ...

  8. Razer Synapse 0 day漏洞可获得Windows 10管理员权限

    Razer Synapse 0 day漏洞,插入Razer鼠标即可获得Windows 10管理员权限. Razer 是一家知名的游戏设备品牌厂商,提供的产品包括游戏鼠标和键盘.在Windows 10或 ...

  9. iOS 10 之后权限设置

    iOS 10 之后权限设置 麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风? 相机权限: Privacy - Camera U ...

最新文章

  1. 武汉首座无人驾驶电动汽车充电站投入使用
  2. java调用百度推送详解,关于百度推送,请教一下大家
  3. andorid 通过包名启动应用
  4. Focus on the Good 专注于好的方面
  5. 想基于K8s按需扩展应用程序,可从这几方面入手
  6. android fragment学习6--FragmentTabHost底部布局
  7. H3C模拟器里的F1060防火墙如何开启WEB界面
  8. mysql显示行号,通过表名模糊查找,通过列名模糊查找,常用sql
  9. Unity3D脚印6——模型动画
  10. 【MDVRP】基于matlab遗传算法求解多仓库车辆路径规划问题【含Matlab源码 1481期】
  11. mysql写保护,sd卡有写保护怎么格式化
  12. 教你三步实现CDH到星环TDH的平滑迁移
  13. ❌ Exiting due to GUEST_PROVISION: Failed to cache ISO: unable to cache ISO:
  14. input获取焦点边框 outline属性
  15. PowerDesigner 15下载(破解)
  16. 一些符号及颜色的英语写法总结
  17. 中国保险中介行业市场规模调研及投资可行性研究报告2022-2027年
  18. 一开机右下角显示无法连接到服务器,win7右下角提示此连接受限制或无连接怎么办...
  19. 设计已读和未读的公告
  20. splint 编译安装

热门文章

  1. 【ArcGIS 10.2新特性】Portal for ArcGIS新特性
  2. # 语音信号处理基础(十)——梅尔倒谱系数
  3. [转载] Python程序将十进制转换为二进制,八进制和十六进制
  4. [转载] python+selenium自动化软件测试(第3章):unittes
  5. [转载] python学习笔记(三)- numpy基础:array及matrix详解
  6. 如何理解FPGA的配置状态字寄存器Status Register
  7. C#方法的六种参数,值参数、引用参数、输出参数、参数数组、命名参数、可选参数...
  8. 【luogu】P1772物流运输(最短路+DP)
  9. Java基础(五):数组和Java方法
  10. yolov3前向传播(二)-- yolov3相关模块的解析与实现(二)