首页 > 解决方案 > 在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());
}

标签: android

解决方案


用下面的替换你的意图块

//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>

推荐阅读