使用Android本地代码(JAVA)在NativeScript中

4
我想在我的NativeScript应用中使用本机Android代码来检查在Play Store是否有更新。我正在使用官方的Android文档 支持应用内更新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.
    }
});

原生脚本代码如下所示:

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: checkForUpdate() 中的错误:Error: 无法将对象转换为Lcom/google/android/play/core/tasks/OnSuccessListener;在索引0处

有人能告诉我我做错了什么吗?

2个回答

3

您需要按照文档部分所示,传递已转换为JavaScript的本机Android侦听器。在您的情况下,您应该根据文章中所示的规则创建成功侦听器。


你需要实现OnSuccessListener接口,就像文档中的OnClickListener一样。 - Manoj

2
这个问题的解决方案如下:
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');
      }
    });

  }



}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接