【发布时间】:2019-07-28 03:51:45
【问题描述】:
我正在使用改造网络服务下载 PDF。然后我显示一个自创建的通知以显示文件已下载。但我无法理解单击通知时如何显示或打开文件。
这是我的通知电话:
RestClient.webServices()
.downloadFile(id)
.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
boolean writtenToDisk = writeResponseBodyToDisk(response.body());
Log.e(TAG, "file download was a success? " + writtenToDisk);
if (writtenToDisk) {
showToast("Invoice downloaded successfully");
showDownloadNotification();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
这是 writeResponseBodyToDisk() 函数:
private boolean writeResponseBodyToDisk(ResponseBody body) {
try {
// todo change the file location/name according to your needs
File futureStudioIconFile = new File(Environment.getExternalStorageDirectory() + File.separator + "/bill.pdf");
InputStream inputStream = null;
OutputStream outputStream = null;
try {
byte[] fileReader = new byte[4096];
long fileSize = body.contentLength();
long fileSizeDownloaded = 0;
inputStream = body.byteStream();
outputStream = new FileOutputStream(futureStudioIconFile);
while (true) {
int read = inputStream.read(fileReader);
if (read == -1) {
break;
}
outputStream.write(fileReader, 0, read);
fileSizeDownloaded += read;
// Log.d(TAG, "file download: " + fileSizeDownloaded + " of " + fileSize);
}
outputStream.flush();
return true;
} catch (IOException e) {
return false;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
} catch (IOException e) {
return false;
}
}
这里是 showDownloadNotification() 函数:
void showDownloadNotification() {
try {
Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "/invoice.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");
// startActivity(intent);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_logo_winds)
.setContentTitle("Invoice downloaded")
.setContentText("")
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(1, builder.build());
} catch (Exception e) {
Log.e(TAG, "Notification " + e.toString());
}
}
所以当我点击创建的通知时,什么也没有发生,当我取消注释 startActivity (intent);它通过说找不到意图的活动而崩溃。 如何通过单击我为它创建的通知打开我下载的文件?
【问题讨论】:
-
@ashazar 是的,我已经看到,无法从我的通知点击中找到我应该在哪里以及如何打开文件/文件夹
标签: android android-intent android-notifications android-pendingintent