1

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?

1
  • Is BleManager a method? That looks like a class, make sure your syntax is correct. Commented Oct 28, 2019 at 20:27

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure when it was introduced, but in newer versions of NativeScript you should use Utils.android.getApplicationContext() to get the application context.

From the documentation:

How to do it in Java:

BatteryManager bm = (BatteryManager) context.getSystemService(BATTERY_SERVICE);
int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

And NativeScript equivalent (including Android and SDK guard):

import { Utils, Device } from '@nativescript/core'

if (global.isAndroid && Device.sdkVersion >= '21') {
  const bm = Utils.android
    .getApplicationContext()
    .getSystemService(android.content.Context.BATTERY_SERVICE)
  const batLevel = bm.getIntProperty(android.os.BatteryManager.BATTERY_PROPERTY_CAPACITY)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.