【问题标题】:Notification pressed to open up file in default app (android)按下通知以在默认应用程序中打开文件(android)
【发布时间】:2014-04-17 02:54:40
【问题描述】:

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

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

谢谢你, 丹尼尔

【问题讨论】:

    标签: android android-intent notifications android-notifications


    【解决方案1】:

    使用未决意图创建这样的通知以打开默认音频播放器

        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);
    

    【讨论】:

    • 为什么创建文件然后获取uri,可以直接解析到uri的路径
    【解决方案2】:

    请将此代码放入您的通知代码中

    Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("content://media/internal/images/media"));
    

    将 Uri 路径更改为放置歌曲的目录

    【讨论】:

      【解决方案3】:

      你试过了吗?

      Intent intent = new Intent();
      intent.setAction(android.content.Intent.ACTION_VIEW);
      File file = new File("/sdcard/download/path/test.mp3");
      intent.setDataAndType(Uri.fromFile(file), "audio/*");
      

      这个意图应该是通知的打开动作。既然你说的是歌曲所以 MIME 将是 audio/* 或者你可以使用 AndroidMimeTypeMap 从文件扩展中获取 mime 类型。

      我已使用此代码打开“使用打开操作”窗口。如果您在手机或平板电脑上设置了默认应用,它将打开默认应用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多