Gaurav Chandra Gaurav Chandra   June 15, 2016

Create persistent notification on Android using NativeScript

I was experimenting with some background tasks in nativescript for android and I found that one way to ensure that your android app will always run even when it is in background is to use a persistent notification. So, I was able to create this using the following code:

NativeScript Code:

main.js

var APP = require('application');
var utils = require("utils/utils");
var application = APP.android;

exports.navigatingTo =  function (args) {
    var page = args.object;

    var randomCode = new java.util.Random().nextInt();//to be used later to associate the 2 things
        var intent = new android.content.Intent(application.foregroundActivity, com.tns.NativeScriptActivity.class);
    var pendingIntent = android.app.PendingIntent.getActivity(application.foregroundActivity, randomCode, intent, android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK);
    var builder = new android.app.Notification.Builder(application.foregroundActivity);
    builder.setDefaults(0);
    builder.setContentTitle("NativeScript Running");
    builder.setContentText("This notification cannot be cleared");
    builder.setContentIntent(pendingIntent);
    builder.setTicker("Persistent Notification");
    builder.setSmallIcon(application.nativeApp.getApplicationInfo().icon);
    builder.setPriority(android.app.Notification.PRIORITY_HIGH);
    builder.setOngoing(true);//this tells the OS that the notification is persistant
    var notification = builder.build();
    var notificationManger = utils.ad.getApplicationContext().getSystemService(android.content.Context.NOTIFICATION_SERVICE);
    notificationManger.notify(randomCode, notification);
};

and voila, you have persistant notification. Ofcourse this can be better managed but now you know how to create a persistent notification. Enjoy.

Gaurav Chandra Gaurav Chandra   March 4, 2016

Get IMEI number of device on Android using NativeScript

There was a small project I did which required the IMEI number to be read. I used the following code to read the IMEI number on Android using NativeScript.

AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

NativeScript code

function getIMEI(){
    var context = utils.ad.getApplicationContext();
    var tm = context.getSystemService(android.content.Context.TELEPHONY_SERVICE);
    return tm.getDeviceId()
}

Then call getIMEI() where there is a need. Simple.

Gaurav Chandra Gaurav Chandra   February 26, 2016

Get SIM Card information on Android using NativeScript

If you need to know sim card information, you can do so very easily in NativeScript for Android. Use the following code:

AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

NativeScript code

function simManage(){
    var tm = android.telephony.TelephonyManager;
    var context = utils.ad.getApplicationContext();
    var manager = context.getSystemService(android.content.Context.TELEPHONY_SERVICE);
    var phoneNumber = manager.getLine1Number();
    var countryCode = manager.getSimCountryIso();
    var simOperator = manager.getSimOperator();
    var carrierName = manager.getSimOperatorName();

    var deviceId = manager.getDeviceId();
    var deviceSoftwareVersion = manager.getDeviceSoftwareVersion();
    var simSerialNumber = manager.getSimSerialNumber();
    var subscriberId = manager.getSubscriberId();

    var callState = manager.getCallState();
    var dataActivity = manager.getDataActivity();
    var networkType = manager.getNetworkType();
    var phoneType = manager.getPhoneType();
    var simState = manager.getSimState();

    var isNetworkRoaming = manager.isNetworkRoaming();

    var mcc = "";
    var mnc = "";

    if (simOperator.length >= 3) {
        mcc = simOperator.substring(0, 3);
        mnc = simOperator.substring(3);
    }
    return {
        phoneNumber:phoneNumber,
        countryCode:countryCode,
        simOperator:simOperator,
        carrierName:carrierName,
        deviceId:deviceId,
        deviceSoftwareVersion:deviceSoftwareVersion,
        simSerialNumber:simSerialNumber,
        subscriberId:subscriberId,
        callState:callState,
        dataActivity:dataActivity,
        networkType:networkType,
        phoneType:phoneType,
        simState:simState,
        isNetworkRoaming:isNetworkRoaming,
        mcc:mcc,
        mnc:mnc
    };
}

When you need the information, call simManage() and it will return the data.

Gaurav Chandra Gaurav Chandra   February 19, 2016

Lock or change orientation on Android using NativeScript

We need to lock or change the device orientation to see a video or play a game etc. You can do so in Android using NativeScript as follows:

function lockOrientation(orientation) {
    var activity = application.startActivity;
    switch (orientation) {
        case 'unlocked':
            activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
            break;

        case 'portrait-primary':
            activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;

        case 'portrait-secondary':
            activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            break;

        case 'landscape-primary':
            activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            break;

        case 'landscape-secondary':
            activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            break;

        case 'portrait':
            activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
            break;

        case 'landscape':
            activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            break;

        default: activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
            break;
    }
}

When ever you need to set or remove the orientation call the function like this:

lockOrientation('unlocked');
or
lockOrientation('landscape');

Gaurav Chandra Gaurav Chandra   February 12, 2016

Make the app private when shown in Android task switcher in NativeScript

Many times we need to hide the information of the app when in task switcher. We can do so using the following code in Android:

var activity = application.startActivity;
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_SECURE);

Just call the above code in your pageloaded event.