Gaurav Chandra Gaurav Chandra   February 10, 2016

Check if the package/app is installed in Android using NativeScript

There are times when you need to open a file in a particular app like Adobe Reader and you want to know whether this app is installed on the device or not.

You can do so using the following code in NativeScript:

function app_installed(packageName){
    var context = utils.ad.getApplicationContext();
    var pm = context.getPackageManager();
    var app_installed = false;
    try {
        pm.getPackageInfo(packageName, android.content.pm.PackageManager.GET_ACTIVITIES);
        app_installed = true;
    }
    catch(e) {
        app_installed = false;
    }
    return app_installed;
}

Just use app_installed('com.facebook.katana') or app_installed('com.adobe.reader') and it will return true or false for your logic.

Gaurav Chandra Gaurav Chandra   February 2, 2016

Show or hide the Android Status Bar in NativeScript

I was trying to get the status bar to hide in one of the apps and created a simple code to do that.

var application = require('application').android;
function statusBar(action){
    var activity = application.startActivity;
    //activity.runOnUiThread(function(){
    var win = activity.getWindow();
    if(action === 'hide'){
        win.addFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN);
    } else if(action === 'show'){
        win.clearFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

Then call statusBar('show') or statusBar('hide') in your pageloaded event.

Gaurav Chandra Gaurav Chandra   February 1, 2016

Create an application shortcut in Android NativeScript

When installing an app, many times there is a need to show the shortcut Icon on the Android home screen. You can do so in Android by adding the following:

AndroidManifest:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

And in your page loaded event for the main view:

var androidApp = applicationModule.android;
var context = utils.ad.getApplicationContext();
var pm = context.getPackageManager();
var i = new android.content.Intent();
i.setClassName(androidApp.packageName, androidApp.startActivity.getClass().getName());
i.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
var shortcutintent = new android.content.Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(android.content.Intent.EXTRA_SHORTCUT_NAME,"Name of the App");

var ri = pm.resolveActivity(i, 0);
var iconId = ri.activityInfo.applicationInfo.icon;
var icon = android.content.Intent.ShortcutIconResource.fromContext(context, iconId);

shortcutintent.putExtra(android.content.Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(android.content.Intent.EXTRA_SHORTCUT_INTENT, i);
context.sendBroadcast(shortcutintent);

Gaurav Chandra Gaurav Chandra   January 11, 2016

Restrict the Text Field to have limited characters

In html input we used to put restriction like this on the number of characters:

<input type="text" maxlength="10" size="10" />

But in nativescript you would need to code it in the code behind file like.

var phoneField = view.getViewById(page,'phoneField');

var fArray = [];

// 10 is the character length I want to restrict
fArray[0] = new android.text.InputFilter.LengthFilter(10);

phoneField.android.setFilters(fArray);

Put the above code in pageloaded or anywhere so that it executes.

Gaurav Chandra Gaurav Chandra   January 8, 2016

How to select a contact in NativeScript Android

While working on an app, I came across a requirement to select contact from the phone. I wanted to open the contact picker and select the contact but NativeScript does not have it inbuilt in the platform.

So, I decided to try my hand and here is the code:

    var intent = new android.content.Intent(android.content.Intent.ACTION_PICK, android.net.Uri.parse("content://contacts"));

    intent.setType(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);

    application.android.foregroundActivity.startActivityForResult(intent, 1);

    application.android.onActivityResult = function (requestCode, resultCode, data) {

    var contactUri = data.getData();

    // We only need the NUMBER column, because there will be only one row in the result
    var projection = [android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER];

    // Perform the query on the contact to get the NUMBER column
    // We don't need a selection or sort order (there's only one result for the given URI)

    var cursor = utils.ad.getApplicationContext().getContentResolver().query(contactUri, projection, null, null, null);

    cursor.moveToFirst();

    // Retrieve the phone number from the NUMBER column
    var column = cursor.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER);

    var number = cursor.getString(column);

Put the above code on button tap event and you are good to go.

Gaurav Chandra
Gaurav Chandra

Gaurav Chandra

I am available for work

Contact me now