如何在 NativeScript 中以编程方式获取应用程序名称。

huangapple go评论105阅读模式
英文:

How to get app name programatically in native script

问题

我正在尝试将一些文件保存在我的本地文件存储中,所以我正在进行类似以下的操作:

var folder_name = "abcde/" + viewModel.dir_path;
const documents = fileSystemModule.knownFolders.documents();
documents._path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
const folder = documents.getFolder(folder_name);
var file = fileSystemModule.path.join(folder._path, this.pdf_url.split("/").pop());
var url = this.pdf_url;

httpModule.getFile(url, file).then(function(result) {
    console.log(result);
    Toast.makeText(`${result._name} 已成功下载到 ${folder_name}`).show();

}, function(e) {
    console.log(e);
});

唯一的问题是硬编码的值 abcde/,我希望它是“应用程序名称”。无论应用程序名称是什么,它都应该使用该名称。

我找不到以编程方式读取应用程序名称的方法。我需要这个在Android上,我对IOS不感兴趣。

英文:

I'm trying to save some files on my local File storage So, I'm doing Something like below

var folder_name = "abcde/" + viewModel.dir_path;
                const documents = fileSystemModule.knownFolders.documents();
                documents._path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
                const folder = documents.getFolder(folder_name);
                var file = fileSystemModule.path.join(folder._path, this.pdf_url.split("/").pop());
                var url = this.pdf_url;

                httpModule.getFile(url, file).then(function(result) {
                    console.log(result);
                    Toast.makeText(`${result._name} is succesfully downloaded in ${folder_name}`).show();

                }, function(e) {
                    console.log(e);
                });

the only problem is the hardcoded value abcde/ I want it to be app name. whatever the app name is it should take that name.

I don't find any ways to read app name programatically. I need this to Android I'm not interested in IOS.

答案1

得分: 0

这可能是一个答案。但这仍然不是一个合适的方式。

const documents = fileSystemModule.knownFolders.currentApp();
console.log(documents); --> "/data/data/org.nativescript.app_name/files/app"
var str = documents;
var arr = str.split("/")[3];
console.log(arr.split(".")[2]); --> app_name
英文:

may be this is one could be the answer. but still this is not a proper way

const documents = fileSystemModule.knownFolders.currentApp();
console.log(documents); --> "/data/data/org.nativescript.app_name/files/app"
var str = documents;
var arr = str.split("/")[3];
console.log(arr.split(".")[2]); ---> app_name

答案2

得分: 0

在Android中,您可以在strings.xml中设置应用程序的应用名称,例如:

<string name="app_name">App_Name</string>

然后,每当您想要重用应用名称时,您可以在应用程序中使用getString方法并提供其名称。

英文:

In Android, you can set the app name for the application in strings.xml, for example:

&lt;string name=&quot;app_name&quot;&gt;&quot;App_Name&quot;&lt;/string&gt;

Then whenever you want to reuse the app name, you can use the getString method with its name in the application.

huangapple
  • 本文由 发表于 2020年1月3日 14:28:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定