问题
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?
回答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 :)
回答2:
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.
来源:https://stackoverflow.com/questions/48539997/how-disable-screenshots-in-nativescript-mobile-application