0

In native Android application for disabling screenshors need to add code

getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);

but how use this security option in NativeScript application?

2 Answers2

1

This solution is done in Nativescript-Vue, please adjust it according to the varient of Nativescript you use.

Import these :

import { isAndroid, isIOS, device, screen} from "tns-core-modules/platform";
const app = require("tns-core-modules/application");

Add a pageLoad function to execute code on page load :

<Page @loaded="pageLoad">

Run this code :

pageLoad: function() {
    if (isAndroid) {
      if (app.android && device.sdkVersion >= '21') {
        const window = app.android.startActivity.getWindow();
        window.setFlags(android.view.WindowManager.LayoutParams.FLAG_SECURE,
                     android.view.WindowManager.LayoutParams.FLAG_SECURE);
      }
    }
}

Personally tested by me, basically import the variables, and run the code provided above on pageload or on page created.

This is my first answer on StackOverflow :)

Norsky
  • 13
  • 5
0

First off since you're going to be working with native code if you haven't I strongly recommend installing tns-platform-declarations. it will help immensely with this process.

after you have that setup

After you have that installed and setup for android.

import { topmost } from 'ui/frame';

if you don't add tns platform declarations then, else you can skip this.

declare const android: any;

then put this code where appropriate

//run this code if only in android application.
if(topmost().android){
    topmost().android.activity.getWindow().setFlags(android.view.WindowManager.LayoutParams.FLAG_SECURE,android.view.WindowManager.LayoutParams.FLAG_SECURE);
}

I haven't run this personally, so I'm not 100% sure it will work, if it doesn't it should get at least get you most of the way there.

JoshSommer
  • 2,550
  • 18
  • 23
  • Hi guys, I have added following code inside the constructor of the appcomponent.ts. But still i can see the app screen visible at the background apps. Please advice where should i use this code? app.android.startActivity.getWindow().setFlags( android.view.WindowManager.LayoutParams.FLAG_SECURE, android.view.WindowManager.LayoutParams.FLAG_SECURE); – smartsanja Apr 23 '18 at 07:05