如何将 android activity 上下文从 typescript 文件传递​​到 nativescript 中的 Java 文件

how to pass android activity context from typescript file to Java file in nativescript

https://www.codeday.top/2017/10/23/52017.html

错误: 但是我在命令提示符下收到无法将对象转换为上下文错误。

app.component.ts:

var activity:any = android.app.Activity;

export class AppComponent {

    onTap(args: EventData) {     

        org.example.MyToast.showToast(activity);  --> Here I'm passing object.So it doesn't take activity context reference. That's why I'm getting error.
    }   

}                                                       

MyToast.java:

public class MyToast {

    public static void showToast(Context context) {  

        final Dialog openDialog = new Dialog(context);
   }
  }

我不知道如何将 nativescript 中的 activity 上下文引用传递给 typescript 文件。谁能帮帮我。

android.app.Activity 不是 Activity 的实例,它是 class 描述符。

您可以在 {N} 官方文档中找到有关访问当前前台 activity 的信息: https://docs.nativescript.org/cookbook/application#tracking-the-current-activity

import * as app from "tns-core-modules/application";
const androidApp = app.android;

if (androidApp.foregroundActivity === androidApp.startActivity) {
    ////console.log("We are currently in the main (start) activity of the application");
}