简体   繁体   中英

use Android Native code (JAVA) in NativeScript

i want to use native android code in my NativeScript app to check if there is update available in play store.

I am using official android docs.

Support in app updates Android

The native java code is below

 // 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.
    }
});

and NavtiveScript code is below

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);
    }
  }

}

and i am getting this error

JS: Err in checkForUpdate() : Error: Cannot convert object to Lcom/google/android/play/core/tasks/OnSuccessListener; at index 0

Can anyone tell me what i am doing wrong.

You need to pass the marshaled (converted from Java to JavaScript) native Android listener as shown in this documentation seciton . In your case, you should create a success listener with the rules shown in the article.

The solution for this problem is below

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');
      }
    });

  }



}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM