点击通知

时间:2016-05-31 04:40:18

标签: android android-intent android-notifications

你好,我是Android开发的新手。我想做的是:

  1. 使用网址下载文件
  2. 将其放入我的下载
  3. 在IntentService中使用下载管理器进行下载
  4. 完成下载后,我正在显示通知
  5. 通知通知用户下载完成
  6. 问题是我希望用户点按生成的通知,然后打开下载文件夹

    我一直在尝试的是:

    public void sendNotification() {
    
            // Use NotificationCompat.Builder to set up our notification.
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    
            //icon appears in device notification bar and right hand corner of notification
            builder.setSmallIcon(R.drawable.downloaded);
    
            // This intent is fired when notification is clicked
            *****Here I want the user if clicks this notification the Download Folder should Open up*****
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    
            Uri imgUri = Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
    
            intent.setDataAndType(imgUri, "file/*");
            startActivity(intent);
    
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    
            // Set the intent that will fire when the user taps the notification.
            builder.setContentIntent(pendingIntent);
    
            // Large icon appears on the left of the notification
            builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.downloaded));
    
            // Content title, which appears in large type at the top of the notification
            builder.setContentTitle("Complete");
    
            // Content text, which appears in smaller text below the title
            builder.setContentText("Your Download has been completed Successfully!");
    
            //set the subtext
            builder.setSubText("Click to open the Downloads Folder!");
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
            // Will display the notification in the notification bar
            notificationManager.notify(NOTIFICATION_ID, builder.build());
        }
    

    每当我点击它时都没有发生任何事情!

    我是Android新手,请仔细检查我的错误,以便我可以了解更多信息。 这不是一项任务,我正在向自己学习! 提前谢谢。

3 个答案:

答案 0 :(得分:0)

试试这个

    Uri selectedUri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(selectedUri, "resource/folder");

    if (intent.resolveActivityInfo(getPackageManager(), 0) != null){
            startActivity(intent);
        }
        else 
        {
          // if you reach this place, it means there is no any file 
          // explorer app installed on your device
        }

View this thread

答案 1 :(得分:0)

检查这个

public void openFolder()
{
  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/myFolder/");
  intent.setDataAndType(uri, "text/csv");
  startActivity(Intent.createChooser(intent, "Open folder"));
}

答案 2 :(得分:0)

我这样做,是这样的:

// Create an explicit intent for an Activity in your app
    val intent = Intent(DownloadManager.ACTION_VIEW_DOWNLOADS).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
    builder = NotificationCompat.Builder(context!!, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_file_word)
        .setContentTitle("Descarga completa")
        .setContentText("Descarga de archivo completa")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT).setContentIntent(pendingIntent)
        .setAutoCancel(true)