How to properly get Android Application Context in Nativescript

Issue

I am creating a Nativescript Application with some specific native handling on Android. As it was needed to initialize a plugin on onCreate() method of Android, I extended Nativescript’s Main Application by following:
https://docs.nativescript.org/core-concepts/android-runtime/advanced-topics/extend-application-activity

I also can confirm that my custom class is properly setted up and working after putting logs and getting on the onCreate() method:

Code before applying needed initialization:

import { isAndroid } from "tns-core-modules/ui/page/page";
import * as application from "tns-core-modules/application";

declare var co;
declare var android;
declare var applicationContext;

// const context = android.content.Context;
const BleManager = co.igloohome.ble.lock.BleManager;
var utilsModule = require("tns-core-modules/utils/utils");

@JavaProxy("org.myApp.Application")
class Application extends android.app.Application {
    onCreate(): void {
        super.onCreate();

        console.log("OnCreate() called");

    }

    attachBaseContext(baseContext: any) { // android.content.Context
        super.attachBaseContext(baseContext);
    }
}

With this code, I was able to see the “OnCreate() called” on the log and that means my extension is working.

Now, I am trying to initialize an SDK, and so I did it based on documentation like so:

import { isAndroid } from "tns-core-modules/ui/page/page";
import * as application from "tns-core-modules/application";

declare var co;
declare var android;
declare var applicationContext;

// const context = android.content.Context;
const BleManager = co.igloohome.ble.lock.BleManager;
var utilsModule = require("tns-core-modules/utils/utils");

@JavaProxy("org.myApp.Application")
class Application extends android.app.Application {
    onCreate(): void {
        super.onCreate();

        console.log("OnCreate() called");

        //Setup Ble SDK
        BleManager(utilsModule.ad.getApplicationContext()).setDebug(true)

        console.log("OnCreate() end");
    }

    attachBaseContext(baseContext: any) { // android.content.Context
        super.attachBaseContext(baseContext);
    }
}

And unfortunately it spits out an error:

JS: OnCreate() called
System.err: java.lang.RuntimeException: Unable to create application org.myApp.Application: com.tns.NativeScriptException:
System.err: Calling js method onCreate failed
System.err:
System.err: Error: Trying to link invalid 'this' to a Java object

I also can confirm that “BleManager” object is not undefined. Only that, I think this might be due to I am passing the wrong Application Context. (The method needs the application context to initialize).

How do you get Android’s Main Application context in Nativescript?

Solution

I believe you can do it like this:

import * as application from '@nativescript/core/application';


const ctx = application.android.context;

or (if you are not using scoped packages):

import * as application from 'tns-core-modules/application';


const ctx = application.android.context;

Answered By – Dmytro Gokun

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published