在 NativeScript 中使用 Android 本机代码 (JAVA)
use Android Native code (JAVA) in NativeScript
我想在我的 NativeScript 应用程序中使用本机 android 代码来检查 Play 商店中是否有可用的更新。
我正在使用官方 android 文档。
Support in app updates Android
原生java代码如下
// Creates instance of the manager.
AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(context);
// Returns an intent object that you use to check for an update.
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
// For a flexible update, use AppUpdateType.FLEXIBLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
// Request the update.
}
});
下面是 NavtiveScript 代码
import { Injectable } from '@angular/core';
import { ad as androidUtilities } from 'tns-core-modules/utils/utils';
declare const com: any;
@Injectable({
providedIn: 'root'
})
export class AppUpdateService {
constructor() {
}
public checkForUpdate() {
try {
const context = androidUtilities.getApplicationContext();
const appUpdateManager = com.google.android.play.core.appupdate.AppUpdateManagerFactory.create(context);
appUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo => {
});
} catch (err) {
console.log('Err in checkForUpdate() : ', err);
}
}
}
我收到了这个错误
JS: Err in checkForUpdate() : Error: Cannot convert object to
Lcom/google/android/play/core/tasks/OnSuccessListener; at index 0
谁能告诉我我做错了什么。
您需要传递编组(从 Java 转换为 Java 脚本)本机 Android 侦听器,如图 in this documentation seciton 所示。在您的情况下,您应该使用文章中显示的规则创建成功侦听器。
这个问题的解决方案如下
import { Injectable } from '@angular/core';
import { ad as androidUtilities } from 'tns-core-modules/utils/utils';
import { AppUpdateAvailability } from '~/app/models/interfaces/app-update-availability.interface';
declare const com: any;
@Injectable({
providedIn: 'root'
})
export class AppUpdateService {
constructor() {
}
public checkForUpdate(): Promise<AppUpdateAvailability> {
return new Promise((res, rej) => {
try {
const context = androidUtilities.getApplicationContext();
const appUpdateManager = com.google.android.play.core.appupdate.AppUpdateManagerFactory.create(context);
const appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
appUpdateInfoTask.addOnSuccessListener(new com.google.android.play.core.tasks.OnSuccessListener({
onSuccess: function (AppUpdateInfo: any) {
const UpdateAvailability = com.google.android.play.core.install.model.UpdateAvailability;
switch (AppUpdateInfo.updateAvailability()) {
case UpdateAvailability.UNKNOWN:
res(AppUpdateAvailability.Unknown);
break;
case UpdateAvailability.UPDATE_NOT_AVAILABLE:
res(AppUpdateAvailability.UpdateNotAvailable);
break;
case UpdateAvailability.UPDATE_AVAILABLE:
res(AppUpdateAvailability.UpdateAvailable);
break;
case UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS:
res(AppUpdateAvailability.DeveloperTriggeredUpdateInProgress);
break;
default:
rej('App update : Something went wrong!');
break;
}
}
}));
} catch (err) {
rej('Err in checkForUpdate() : Code error');
}
});
}
}
我想在我的 NativeScript 应用程序中使用本机 android 代码来检查 Play 商店中是否有可用的更新。
我正在使用官方 android 文档。
Support in app updates Android
原生java代码如下
// Creates instance of the manager.
AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(context);
// Returns an intent object that you use to check for an update.
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
// For a flexible update, use AppUpdateType.FLEXIBLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
// Request the update.
}
});
下面是 NavtiveScript 代码
import { Injectable } from '@angular/core';
import { ad as androidUtilities } from 'tns-core-modules/utils/utils';
declare const com: any;
@Injectable({
providedIn: 'root'
})
export class AppUpdateService {
constructor() {
}
public checkForUpdate() {
try {
const context = androidUtilities.getApplicationContext();
const appUpdateManager = com.google.android.play.core.appupdate.AppUpdateManagerFactory.create(context);
appUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo => {
});
} catch (err) {
console.log('Err in checkForUpdate() : ', err);
}
}
}
我收到了这个错误
JS: Err in checkForUpdate() : Error: Cannot convert object to Lcom/google/android/play/core/tasks/OnSuccessListener; at index 0
谁能告诉我我做错了什么。
您需要传递编组(从 Java 转换为 Java 脚本)本机 Android 侦听器,如图 in this documentation seciton 所示。在您的情况下,您应该使用文章中显示的规则创建成功侦听器。
这个问题的解决方案如下
import { Injectable } from '@angular/core';
import { ad as androidUtilities } from 'tns-core-modules/utils/utils';
import { AppUpdateAvailability } from '~/app/models/interfaces/app-update-availability.interface';
declare const com: any;
@Injectable({
providedIn: 'root'
})
export class AppUpdateService {
constructor() {
}
public checkForUpdate(): Promise<AppUpdateAvailability> {
return new Promise((res, rej) => {
try {
const context = androidUtilities.getApplicationContext();
const appUpdateManager = com.google.android.play.core.appupdate.AppUpdateManagerFactory.create(context);
const appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
appUpdateInfoTask.addOnSuccessListener(new com.google.android.play.core.tasks.OnSuccessListener({
onSuccess: function (AppUpdateInfo: any) {
const UpdateAvailability = com.google.android.play.core.install.model.UpdateAvailability;
switch (AppUpdateInfo.updateAvailability()) {
case UpdateAvailability.UNKNOWN:
res(AppUpdateAvailability.Unknown);
break;
case UpdateAvailability.UPDATE_NOT_AVAILABLE:
res(AppUpdateAvailability.UpdateNotAvailable);
break;
case UpdateAvailability.UPDATE_AVAILABLE:
res(AppUpdateAvailability.UpdateAvailable);
break;
case UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS:
res(AppUpdateAvailability.DeveloperTriggeredUpdateInProgress);
break;
default:
rej('App update : Something went wrong!');
break;
}
}
}));
} catch (err) {
rej('Err in checkForUpdate() : Code error');
}
});
}
}