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 hiện có.
Định cấu hình ứng dụng của bạn là Bắt buộc sử dụng công nghệ AR hoặc Không bắt buộc sử dụng công nghệ AR
Để tiết kiệm dung lượng trên từng thiết bị, tất cả tính năng AR đều được lưu trữ trong một ứng dụng có tên là Dịch vụ Google Play cho AR. Ứ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 AR bằng SDK ARCore. Bạn có thể định cấu hình một ứng dụng hỗ trợ tính năng AR theo hai cách: Bắt buộc phải có AR và Không bắt buộc phải có AR. Chỉ định này xác định cách ứng dụng tương tác với ứng dụng Dịch vụ Google Play cho AR.
Ứ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 AR.
- Cửa hàng Google Play sẽ chỉ cung cấp các ứng dụng Yêu cầu AR trên những thiết bị hỗ trợ ARCore.
- Khi người dùng cài đặt một ứng dụng Yêu cầu AR, Cửa hàng Google Play sẽ tự động cài đặt Dịch vụ Google Play cho AR 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 bước kiểm tra thời gian chạy bổ sung trong trường hợp Dịch vụ Google Play cho AR đã lỗi thời hoặc đã bị gỡ cài đặt theo cách thủ công.
Ứng dụng Không bắt buộc sử dụng AR sử dụng ARCore để nâng cao chức năng hiện có. Ứng dụng này có các tính năng AR không bắt buộc, chỉ được kích hoạt trên các thiết bị hỗ trợ ARCore đã cài đặt Dịch vụ Google Play cho AR.
- Bạn có thể cài đặt và chạy các ứng dụng Không bắt buộc về AR trên các thiết bị không hỗ trợ ARCore.
- Khi người dùng cài đặt một ứng dụng Không bắt buộc về AR, Cửa hàng Google Play sẽ không tự động cài đặt Dịch vụ Google Play cho AR trên thiết bị.
Bắt buộc phải có AR | Không bắt buộc sử dụng công nghệ AR | |
---|---|---|
Sử dụng tính năng AR | Ứng dụng của bạn cần có 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ỉ được liệt kê 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 sử dụng
ArCoreApk.requestInstall()
để tải xuống và cài đặt ARCore. |
Yêu cầu đối với minSdkVersion của Android |
Android 7.0 (API cấp 24) | Android 4.4 (API cấp 19), mặc dù để chạy bất kỳ chức năng AR nào, bạn cần có ít nhất 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 cài đặt và hỗ trợ 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 sử dụng AR hoặc Không bắt buộc sử dụng AR, hãy cập nhật AndroidManifest.xml
để thêm các mục sau:
<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>
<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.gradle
của dự án có chứa 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.gradle
của ứng dụng.dependencies { … implementation 'com.google.ar:core:1.33.0' }
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 rằng các tính năng AR trên ứng dụng của bạn chạy trơn tru.
Kiểm tra xem thiết bị có hỗ trợ ARCore hay không
Cả ứng dụng Yêu cầu AR và Không bắt buộc AR đề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 nên tắt chức năng liên quan đến AR và ẩn các thành phần giao diện người dùng liên quan.
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
}
}
}
@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 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, hàm này sẽ trả về UNKNOWN_CHECKING
. Để giảm độ trễ và thời gian bật lên, ứng dụng nên gọi ArCoreApk.checkAvailability()
một lần vào đầu vòng đời để bắt đầu truy vấn, bỏ qua giá trị được trả về. Bằng cách này, kết quả đã lưu trong bộ nhớ đệm sẽ có sẵn ngay lập tức khi một thành phần giao diện người dùng nhập AR có thể hiển thị.
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 Bắt buộc sử dụng AR và Không bắt buộc sử dụng AR đều phải sử dụng
ArCoreApk.requestInstall()
trước khi tạo phiên ARCore để kiểm tra xem phiên bản Dịch vụ Google Play cho AR có tương thích và (vẫn) được cài đặt hay không, cũng như để đảm bảo rằng tất cả dữ liệu hồ sơ thiết bị ARCore bắt buộc đều đã được tải xuống.
// 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.
}
…
}
// 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 quyền truy cập vào máy ảnh
Cả ứng dụng Không bắt buộc sử dụng AR và Bắt buộc sử dụng AR đều phải đảm bảo rằng quyền truy cập vào máy ảnh đã được cấp trước khi tạo Phiên AR.
override fun onResume() {
super.onResume()
// ARCore requires camera permission to operate.
if (!CameraPermissionHelper.hasCameraPermission(this)) {
CameraPermissionHelper.requestCameraPermission(this)
return
}
…
}
@Override
protected void onResume() {
super.onResume();
// ARCore requires camera permission to operate.
if (!CameraPermissionHelper.hasCameraPermission(this)) {
CameraPermissionHelper.requestCameraPermission(this);
return;
}
…
}
Hoạt động AR của bạn cũng phải triển khai onRequestPermissionsResult()
.
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()
}
}
@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
Để phát hành ứ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 phiên ARCore.