Property 'content' does not exist on type 'AndroidApplication'

nativescriptcore

#1

import { Component } from “@angular/core"
import { Page } from “ui/page”;
import { android } from “tns-core-modules/application”;
// import { android } from “tns-core-modules/application”;
@Component({
moduleId: module.id,
selector: “verification”,
templateUrl: “./verification.html”
})
export class Verification {
constructor() {
this.receivers();
}
public receivers() {
let app =android;
//// Register the broadcast receiver
if (app.android) {
android
app.android.registerBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED,
function onReceiveCallback(context: any, intent: any) {
console.log(”==================================================");
console.log(JSON.stringify(context));
console.log("==================================================");
console.log(JSON.stringify(intent));
console.log("==================================================");
// var level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1);
// var scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1);
// var percent = (level / scale) * 100.0;
////console.log("Battery: " + percent + “%”);
});
}
}
}

I am trying to execute this code sample by nativescript website for brodcastreceiver. But it doesn’t working. It throws an error “Property ‘content’ does not exist on type ‘AndroidApplication’”. And went through the packege but there is no any property found.
Can any one help me to continue???


#2

Instead of using the android package you are using android application instance. You don’t have to import android package from anywhere, you will just have it available by default at runtime.

If you face typescript error, you can just declare it like declare var android: any;. At run time it will point to the android package Or you can add tns-platform-declarations to your dev dependencies.


#3

Thanks!!!

if (app.android) {
app.android.registerBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED,
function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
var level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1);
var scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1);
var percent = (level / scale) * 100.0;
////console.log("Battery: " + percent + “%”);
});
}

But now its giving error at bolded line as “Cannot find namespace ‘android’”.
Can you help me???


#4

Thank you very much. You solved my problem…