如何在用户点击 android 中的通知时打开文件?
How to open file when user click's on notification in android?
我正在向 android 中的用户显示文件下载通知。
现在我想在用户单击该通知时打开该文件。
我已经使打开文件的方法工作正常。我只是不知道如何在通知上设置点击侦听器来调用此方法。
我的通知创建方法
public void displayFileDownloadNotification(Context context, String fileName)
{
new CommonMethod().createNotificationChannel(context);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
builder.setSmallIcon(R.mipmap.cghs_launcher_logo);
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.cghs_launcher_logo));
builder.setContentTitle("CGHS");
builder.setContentText("Downloaded "+fileName);
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(NOTIFICATION_ID++, builder.build());
}
public void createNotificationChannel(Context context)
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
CharSequence name = "Attachment Download Notification";
String description = "Simple Description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
notificationChannel.setDescription(description);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}
还有我的文件打开方式
public static void openDownloadedFile(String fileName, Context context)
{
try{
String path = Environment.getExternalStorageDirectory().getAbsolutePath().concat("/Download/" + fileName);
Log.e("File Path=",path);
File file = new File(path);
if (file.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent target = Intent.createChooser(intent, "Open File");
try {
context.startActivity(target);
} catch (Exception e)
{
Toast.makeText(context, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
} else {
Log.e("File = ", "File does not exits");
}
} catch (Exception e)
{
Log.e("Error ",e.getMessage());
Toast.makeText(context, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
现在如何在单击通知时调用此 openDownloadedFile。
新建Activity
在 OnCreate
中调用 openDownloadedFile
编辑您的通知:
Intent intent = new Intent(this, NewActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);
我正在向 android 中的用户显示文件下载通知。
现在我想在用户单击该通知时打开该文件。
我已经使打开文件的方法工作正常。我只是不知道如何在通知上设置点击侦听器来调用此方法。
我的通知创建方法
public void displayFileDownloadNotification(Context context, String fileName)
{
new CommonMethod().createNotificationChannel(context);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
builder.setSmallIcon(R.mipmap.cghs_launcher_logo);
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.cghs_launcher_logo));
builder.setContentTitle("CGHS");
builder.setContentText("Downloaded "+fileName);
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(NOTIFICATION_ID++, builder.build());
}
public void createNotificationChannel(Context context)
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
CharSequence name = "Attachment Download Notification";
String description = "Simple Description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
notificationChannel.setDescription(description);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}
还有我的文件打开方式
public static void openDownloadedFile(String fileName, Context context)
{
try{
String path = Environment.getExternalStorageDirectory().getAbsolutePath().concat("/Download/" + fileName);
Log.e("File Path=",path);
File file = new File(path);
if (file.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent target = Intent.createChooser(intent, "Open File");
try {
context.startActivity(target);
} catch (Exception e)
{
Toast.makeText(context, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
} else {
Log.e("File = ", "File does not exits");
}
} catch (Exception e)
{
Log.e("Error ",e.getMessage());
Toast.makeText(context, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
现在如何在单击通知时调用此 openDownloadedFile。
新建Activity 在 OnCreate
中调用openDownloadedFile
编辑您的通知:
Intent intent = new Intent(this, NewActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);