Bật AR để sử dụng các tính năng thực tế tăng cường trong ứng dụng mới hoặc ứng dụng hiện có.
Định cấu hình ứng dụng của bạn thành AR Required (Bắt buộc phải có AR) hoặc AR Optional (Không bắt buộc phải có AR)
Để tiết kiệm dung lượng trên từng thiết bị, tất cả các tính năng thực tế tăng cường đều được lưu trữ trong một ứng dụng có tên là Dịch vụ Google Play cho Thực tế tăng cường. Ứng dụng này được Cửa hàng Play cập nhật riêng. Các ứng dụng Android sử dụng tính năng AR giao tiếp với Dịch vụ Google Play cho Thực tế tăng cường bằng SDK ARCore. Bạn có thể định cấu hình một ứng dụng hỗ trợ các tính năng AR theo 2 cách: AR bắt buộc và AR không bắt buộc. Chỉ định này xác định cách ứng dụng thực tế tăng cường tương tác với ứng dụng Dịch vụ Google Play cho Thực tế tăng cường.
Ứng dụng Yêu cầu AR không thể hoạt động nếu không có ARCore. Ứng dụng này yêu cầu thiết bị có hỗ trợ ARCore đã cài đặt Dịch vụ Google Play cho Thực tế tăng cường.
- Cửa hàng Google Play sẽ chỉ cung cấp các ứng dụng AR Required trên những thiết bị hỗ trợ ARCore.
- Khi người dùng cài đặt một ứng dụng Bắt buộc phải có AR, Cửa hàng Google Play sẽ tự động cài đặt Dịch vụ Google Play cho Thực tế tăng cường trên thiết bị của họ. Tuy nhiên, ứng dụng của bạn vẫn phải thực hiện các quy trình kiểm tra bổ sung trong thời gian chạy trong trường hợp Dịch vụ Google Play cho Thực tế tăng cường đã lỗi thời hoặc bị gỡ cài đặt theo cách thủ công.
Ứng dụng AR Optional sử dụng ARCore để cải thiện chức năng hiện có. Ứng dụng này có các tính năng thực tế tăng cường không bắt buộc và chỉ được kích hoạt trên các thiết bị có hỗ trợ ARCore đã cài đặt Dịch vụ Google Play cho Thực tế tăng cường.
- Bạn có thể cài đặt và chạy các ứng dụng AR Optional trên những thiết bị không hỗ trợ ARCore.
- Khi người dùng cài đặt một ứng dụng AR Optional, Cửa hàng Google Play sẽ không tự động cài đặt Dịch vụ Google Play cho Thực tế tăng cường trên thiết bị.
| Yêu cầu về thực tế tăng cường | AR không bắt buộc | |
|---|---|---|
| Mức sử dụng tính năng AR | Ứng dụng của bạn cần ARCore để có chức năng cơ bản. | ARCore tăng cường chức năng của ứng dụng. Ứng dụng của bạn có thể chạy mà không cần hỗ trợ ARCore. |
| Trạng thái hiển thị trên Cửa hàng Play | Ứng dụng của bạn chỉ xuất hiện trong Cửa hàng Play trên những thiết bị hỗ trợ ARCore. | Ứng dụng của bạn tuân theo quy trình đăng thông thường. |
| Phương thức cài đặt Dịch vụ Google Play cho Thực tế tăng cường | Cửa hàng Play sẽ cài đặt Dịch vụ Google Play cho Thực tế tăng cường cùng với ứng dụng của bạn. | Ứng dụng của bạn dùng
ArCoreApk.requestInstall()
để tải xuống và cài đặt ARCore. |
Yêu cầu đối với Android minSdkVersion |
Android 7.0 (API cấp 24) | Android 4.4 (API cấp 19), mặc dù chạy mọi chức năng AR đều yêu cầu tối thiểu Android 7.0 (API cấp 24) |
Phải sử dụng ArCoreApk.checkAvailability() hoặc ArCoreApk.checkAvailabilityAsync() để kiểm tra trạng thái hỗ trợ và cài đặt ARCore
|
||
Phải sử dụng
ArCoreApk.requestInstall()
để cài đặt Dịch vụ Google Play cho Thực tế tăng cường |
Để đặt ứng dụng của bạn là Bắt buộc hỗ trợ AR hoặc Không bắt buộc hỗ trợ AR, hãy cập nhật AndroidManifest.xml để thêm các mục sau:
Yêu cầu về thực tế tăng cường
<uses-permission android:name="android.permission.CAMERA" />
<!-- Limits app visibility in the Google Play Store to ARCore supported devices
(https://developers.google.com/ar/devices). -->
<uses-feature android:name="android.hardware.camera.ar" />
<application …>
…
<!-- "AR Required" app, requires "Google Play Services for AR" (ARCore)
to be installed, as the app does not include any non-AR features. -->
<meta-data android:name="com.google.ar.core" android:value="required" />
</application>
AR không bắt buộc
<uses-permission android:name="android.permission.CAMERA" />
<!-- If your app was previously AR Required, don't forget to remove the
`<uses-feature android:name="android.hardware.camera.ar" />` entry, as
this would limit app visibility in the Google Play Store to only
ARCore supported devices. -->
<application …>
…
<!-- "AR Optional" app, contains non-AR features that can be used when
"Google Play Services for AR" (ARCore) is not available. -->
<meta-data android:name="com.google.ar.core" android:value="optional" />
</application>
Sau đó, hãy sửa đổi build.gradle của ứng dụng để chỉ định minSdkVersion tối thiểu là 24:
android {
defaultConfig {
…
minSdkVersion 24
}
}
Thêm phần phụ thuộc của bản dựng
Để thêm ARCore vào dự án Android Studio, hãy làm như sau:
Đảm bảo tệp
build.gradlecủa dự án có kho lưu trữ Maven của Google.allprojects { repositories { google() … } }Thêm thư viện ARCore mới nhất làm phần phụ thuộc trong tệp
build.gradlecủa ứng dụng.dependencies { … implementation 'com.google.ar:core:1.33.0' }
Thực hiện các bước kiểm tra thời gian chạy
Trong thời gian chạy, hãy thực hiện những việc sau để đảm bảo các tính năng AR trên ứng dụng của bạn chạy mượt mà.
Kiểm tra xem ARCore có được hỗ trợ hay không
Cả ứng dụng AR Required và AR Optional đều phải sử dụngArCoreApk.checkAvailability() hoặc ArCoreApk.checkAvailabilityAsync() để xác định xem thiết bị hiện tại có hỗ trợ ARCore hay không. Trên các thiết bị không hỗ trợ ARCore, ứng dụng phải tắt chức năng liên quan đến thực tế tăng cường và ẩn các phần tử liên quan trên giao diện người dùng.
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Enable AR-related functionality on ARCore supported devices only.
maybeEnableArButton()
…
}
fun maybeEnableArButton() {
ArCoreApk.getInstance().checkAvailabilityAsync(this) { availability ->
if (availability.isSupported) {
mArButton.visibility = View.VISIBLE
mArButton.isEnabled = true
} else { // The device is unsupported or unknown.
mArButton.visibility = View.INVISIBLE
mArButton.isEnabled = false
}
}
}
Java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Enable AR-related functionality on ARCore supported devices only.
maybeEnableArButton();
…
}
void maybeEnableArButton() {
ArCoreApk.getInstance().checkAvailabilityAsync(this, availability -> {
if (availability.isSupported()) {
mArButton.setVisibility(View.VISIBLE);
mArButton.setEnabled(true);
} else { // The device is unsupported or unknown.
mArButton.setVisibility(View.INVISIBLE);
mArButton.setEnabled(false);
}
});
}
ArCoreApk.checkAvailability() hoặc ArCoreApk.checkAvailabilityAsync() để kiểm tra khả năng hỗ trợ ARCore sẽ đảm bảo trải nghiệm nhất quán.
ArCoreApk.checkAvailability() có thể cần truy vấn các tài nguyên mạng để xác định xem thiết bị có hỗ trợ ARCore hay không. Trong thời gian này, phương thức sẽ trả về UNKNOWN_CHECKING. Để giảm độ trễ cảm nhận và hiện tượng xuất hiện đột ngột, các ứng dụng nên gọi ArCoreApk.checkAvailability() một lần ở giai đoạn đầu của vòng đời để bắt đầu truy vấn, bỏ qua giá trị được trả về. Nhờ đó, kết quả được lưu vào bộ nhớ đệm sẽ có sẵn ngay khi một phần tử trên giao diện người dùng nhập AR có thể xuất hiện.
Kiểm tra xem bạn đã cài đặt Dịch vụ Google Play cho Thực tế tăng cường hay chưa
Cả ứng dụng AR Required và AR Optional đều phải sử dụng
ArCoreApk.requestInstall()
trước khi tạo một phiên ARCore để kiểm tra xem phiên bản tương thích của Dịch vụ Google Play cho Thực tế tăng cường có (vẫn) được cài đặt hay không và để đảm bảo rằng tất cả dữ liệu hồ sơ thiết bị ARCore bắt buộc đã được tải xuống.
Kotlin
// requestInstall(Activity, true) will triggers installation of
// Google Play Services for AR if necessary.
var mUserRequestedInstall = true
override fun onResume() {
super.onResume()
// Check camera permission.
…
// Ensure that Google Play Services for AR and ARCore device profile data are
// installed and up to date.
try {
if (mSession == null) {
when (ArCoreApk.getInstance().requestInstall(this, mUserRequestedInstall)) {
ArCoreApk.InstallStatus.INSTALLED -> {
// Success: Safe to create the AR session.
mSession = Session(this)
}
ArCoreApk.InstallStatus.INSTALL_REQUESTED -> {
// When this method returns `INSTALL_REQUESTED`:
// 1. ARCore pauses this activity.
// 2. ARCore prompts the user to install or update Google Play
// Services for AR (market://details?id=com.google.ar.core).
// 3. ARCore downloads the latest device profile data.
// 4. ARCore resumes this activity. The next invocation of
// requestInstall() will either return `INSTALLED` or throw an
// exception if the installation or update did not succeed.
mUserRequestedInstall = false
return
}
}
}
} catch (e: UnavailableUserDeclinedInstallationException) {
// Display an appropriate message to the user and return gracefully.
Toast.makeText(this, "TODO: handle exception " + e, Toast.LENGTH_LONG)
.show()
return
} catch (…) {
…
return // mSession remains null, since session creation has failed.
}
…
}
Java
// requestInstall(Activity, true) will trigger installation of
// Google Play Services for AR if necessary.
private boolean mUserRequestedInstall = true;
@Override
protected void onResume() {
super.onResume();
// Check camera permission.
…
// Ensure that Google Play Services for AR and ARCore device profile data are
// installed and up to date.
try {
if (mSession == null) {
switch (ArCoreApk.getInstance().requestInstall(this, mUserRequestedInstall)) {
case INSTALLED:
// Success: Safe to create the AR session.
mSession = new Session(this);
break;
case INSTALL_REQUESTED:
// When this method returns `INSTALL_REQUESTED`:
// 1. ARCore pauses this activity.
// 2. ARCore prompts the user to install or update Google Play
// Services for AR (market://details?id=com.google.ar.core).
// 3. ARCore downloads the latest device profile data.
// 4. ARCore resumes this activity. The next invocation of
// requestInstall() will either return `INSTALLED` or throw an
// exception if the installation or update did not succeed.
mUserRequestedInstall = false;
return;
}
}
} catch (UnavailableUserDeclinedInstallationException e) {
// Display an appropriate message to the user and return gracefully.
Toast.makeText(this, "TODO: handle exception " + e, Toast.LENGTH_LONG)
.show();
return;
} catch (…) {
…
return; // mSession remains null, since session creation has failed.
}
…
}
Yêu cầu cấp quyền truy cập camera
Cả ứng dụng AR Optional và AR Required đều phải đảm bảo rằng quyền truy cập camera đã được cấp trước khi tạo một phiên thực tế tăng cường.
Kotlin
override fun onResume() {
super.onResume()
// ARCore requires camera permission to operate.
if (!CameraPermissionHelper.hasCameraPermission(this)) {
CameraPermissionHelper.requestCameraPermission(this)
return
}
…
}
Java
@Override
protected void onResume() {
super.onResume();
// ARCore requires camera permission to operate.
if (!CameraPermissionHelper.hasCameraPermission(this)) {
CameraPermissionHelper.requestCameraPermission(this);
return;
}
…
}
Hoạt động thực tế tăng cường của bạn cũng phải triển khai onRequestPermissionsResult().
Kotlin
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
results: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, results)
if (!CameraPermissionHelper.hasCameraPermission(this)) {
Toast.makeText(this, "Camera permission is needed to run this application", Toast.LENGTH_LONG)
.show()
if (!CameraPermissionHelper.shouldShowRequestPermissionRationale(this)) {
// Permission denied with checking "Do not ask again".
CameraPermissionHelper.launchPermissionSettings(this)
}
finish()
}
}
Java
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) {
super.onRequestPermissionsResult(requestCode, permissions, results);
if (!CameraPermissionHelper.hasCameraPermission(this)) {
Toast.makeText(this, "Camera permission is needed to run this application", Toast.LENGTH_LONG)
.show();
if (!CameraPermissionHelper.shouldShowRequestPermissionRationale(this)) {
// Permission denied with checking "Do not ask again".
CameraPermissionHelper.launchPermissionSettings(this);
}
finish();
}
}
Tuân thủ các yêu cầu về quyền riêng tư của người dùng
Để xuất bản ứng dụng trên Cửa hàng Play, hãy đảm bảo rằng ứng dụng của bạn tuân thủ Yêu cầu về quyền riêng tư của người dùng của ARCore.
Bước tiếp theo
- Tìm hiểu cách định cấu hình một phiên ARCore.