platform.ready() 方法未在使用 ionic 的 Android 设备中触发
Posted
技术标签:
【中文标题】platform.ready() 方法未在使用 ionic 的 Android 设备中触发【英文标题】:platform.ready() method is not firing in Android device using ionic 【发布时间】:2021-09-15 23:47:39 【问题描述】:我正在开发一个可以上传、下载和删除附件的 Ionic 应用程序 这里我使用平台内部的文件插件。 ready() 并构建 APK。当我在 android 设备上运行 APK 时,Platform.ready 中的代码没有被触发是什么问题???
import Platform from '@ionic/angular';
import File from '@ionic-native/file/ngx';
constructor(private cFile: File,private platform: Platform)
DownloadAttachment()
var buffer = pFilebytearray.replace(/\"/g, "");
var byteCharacters = atob(buffer);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++)
byteNumbers[i] = byteCharacters.charCodeAt(i);
this.cFile.dataDirectory
var byteArray = new Uint8Array(byteNumbers);
let aType: any = "";
let aMimeTypeJson = as any;
aMimeTypeJson = this.GetMimeType(pAttachmentType.toLowerCase())
aType = aMimeTypeJson.MimeType;
var blob1 = new Blob([byteArray], type: aType );
alert("completed the Name Seperation and Blob Creation")
this.platform.ready().then(() =>
alert("Download Starts...");
let result = this.cFile.createDir(this.cFile.externalDataDirectory, "Documents", true).then(data =>
let dirpath = data.toURL();
alert("Dir created at" + dirpath);
this.cFile.writeFile(dirpath, pAttachmentName, blob1, replace: true )
.then((Result) =>
this.MessageDialog("Message", "Attachment downloaded " + dirpath, false, true);
, (error) =>
this.MessageDialog("Message", "Attachment not downloaded", false, true);
);
).catch(error =>
alert("error" + error);
);
)
【问题讨论】:
platform.ready()
需要什么?尝试使用platform.ready()
下载您的文件。或将您的完整功能包装在platform.ready()
【参考方案1】:
使用异步后为我解决的错误,等待。这使得代码等到平台准备好
async AttachmentDownload(pFilebytearray, pAttachmentType, pAttachmentName): Promise<void>
alert("entering AttachmentDownload")
var buffer = pFilebytearray.replace(/\"/g, "");
var byteCharacters = atob(buffer);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++)
byteNumbers[i] = byteCharacters.charCodeAt(i);
this.cFile.dataDirectory
var byteArray = new Uint8Array(byteNumbers);
let aType: any = "";
let aMimeTypeJson = as any;
aMimeTypeJson = this.GetMimeType(pAttachmentType.toLowerCase())
aType = aMimeTypeJson.MimeType;
var blob1 = new Blob([byteArray], type: aType );
alert("completed the Name Seperation and Blob Creation")
await this.platform.ready().then(() =>
alert("Download Starts...");
let result = this.cFile.createDir(this.cFile.externalDataDirectory, "Documents", true).then(data =>
let dirpath = data.toURL();
alert("Dir created at" + dirpath);
this.cFile.writeFile(dirpath, pAttachmentName, blob1, replace: true )
.then((Result) =>
this.MessageDialog("Message", "Attachment downloaded " + dirpath, false, true);
, (error) =>
this.MessageDialog("Message", "Attachment not downloaded", false, true);
);
).catch(error =>
alert("error" + error);
);
)
【讨论】:
以上是关于platform.ready() 方法未在使用 ionic 的 Android 设备中触发的主要内容,如果未能解决你的问题,请参考以下文章
Mobilefirst PushNotification:在服务器上使用 wl_anonymousUserRealm 时,应用程序未在单击通知时调用 onReceive 方法