gpt4 book ai didi

android - 按下通知以在默认应用程序(android)中打开文件

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:55 25 4
gpt4 key购买 nike

我目前收到一个跟踪下载(歌曲)进度的定期通知。下载完成后,如果用户按下通知,它会在处理该文件类型的默认应用程序中打开歌曲文件。

我尝试过搜索,但不确定在使用 .setDataAndType() 时如何处理 PendingIntent 和 MIME 类型。有人可以解释或提供一些示例代码,说明我如何设置 Intent 以在默认应用程序中从特定文件路径打开文件吗?

谢谢,丹尼尔

最佳答案

像这样创建带有打开默认音频播放器的未决 Intent 的通知

    Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("YOUR_SONG_URI"); // set your audio path
intent.setDataAndType(Uri.fromFile(file), "audio/*");

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

Notification noti = new NotificationCompat.Builder(this)
.setContentTitle("Download completed")
.setContentText("Song name")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).build();

noti.flags |= Notification.FLAG_AUTO_CANCEL;

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);

关于android - 按下通知以在默认应用程序(android)中打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23123767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com