安卓定位權限,位置權限 android_Android 10 –位置權限

 2023-11-19 阅读 16 评论 0

摘要:位置權限 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 fr

位置權限 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)

android9.0、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"/>

android7.0,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 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 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

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/183018.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息