android - 在android中单击通知时打开csv文件
问题描述
4
我想在单击通知时下载 csv 文件。
public static void notifyNotification(String title, String description, File file, Context activity) {
NotificationManager notificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = getNotificationBuilder(activity, description);
Uri uri;
if (Build.VERSION.SDK_INT < 24) {
uri = Uri.fromFile(file);
} else {
uri = Uri.parse(file.getPath()); // My work-around for new SDKs, causes ActivityNotFoundException in API 10.
}
Intent viewFile = new Intent(Intent.ACTION_VIEW);
viewFile.setDataAndType(uri, "application/*");
PendingIntent pIntent = PendingIntent.getActivity(activity, 0, viewFile, 0);
builder = builder
.setSmallIcon(R.mipmap.pay_aw_app_icon)
.setContentTitle(title)
// .setTicker("Ticker")
.setContentText(description)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
.setContentIntent(pIntent);
notificationManager.notify(createID(), builder.build());
}
解决方案
用下面的替换你的意图块
//Make file available via FileProvider
Intent viewFile = new Intent(Intent.ACTION_VIEW);
viewFile.addCategory(Intent.CATEGORY_OPENABLE);
viewFile.setDataAndType(FileProvider.getUriForFile(activity, activity.getApplicationContext().getPackageName() + ".provider", file), "text/csv");
viewFile.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
viewFile.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(activity, 0, viewFile, 0);
在清单中添加提供程序
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
--在res文件夹(res/xml)下 创建文件夹xml
-- 在 xml 文件夹中创建一个名为provider_paths.xml的文件并添加以下内容
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
推荐阅读
- python - 如何在 Tkinter 中使用相同的根目录但不同的文件制作按钮?
- python - 在python中重用聚类算法
- python - 为什么 except 元素无法识别变量“结果”
- java - 基于百分位的 API 调用超时设置
- java - 将 JSON 压缩到 GZIP 并上传到 S3
- java - onServiceConnected 和 onAccessibilityEvent 永远不会被调用
- c - 优化级别的宏 (ARMCC V6)
- python-3.x - selenium 无法定位元素 - 我试过按名称查找、按类名查找、按 xpath 查找,但都返回无法定位元素
- html - 如何在 css3 中缩小一个元素同时放大另一个元素
- sql - 大文件导出到 postgreSQL