单击下载通知时如何浏览到目标文件夹

时间:2015-06-25 15:33:53

标签: android android-notifications

从我的应用程序中,我正在下载一些音乐文件并将其保存在文件夹中。在通知面板中显示下载进度。下载完成后,如果点击通知,我的Android设备应该直接浏览到目标文件夹,类似于"转到文件夹"任何下载完成后在PC上

我是从下面的代码尝试过的,但是它没有用。它只是打开应用程序。

public class Temp extends Activity {

    String[] link;
    String[] items;
    String song_link;
    String song_name;
    int song_index;
    int link_index;
    String url_link;
    Exception error;
    private ProgressBar mProgress;
    long total = 0;
    boolean downloadStatus = false;
    ProgressDialog progressBar;  
    private Handler progressBarHandler = new Handler();  
    int progressBarStatus = 0; 
    Uri selectedUri;
    Intent intent;
    PendingIntent resultPendingIntent;



    NotificationCompat.Builder mBuilder= 
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("My notification")
            .setContentText("Downloading");
    NotificationManager mNotificationManager;

    int id=1;


    private class DownloadFilesTask extends AsyncTask<String, Integer, Long> {
         protected Long doInBackground(String... urls) {
             mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            // mProgress = (ProgressBar) findViewById(R.id.progress_bar);
             url_link = urls[0];
                 try {
                    /* runOnUiThread(new Runnable() {
                        public void run() {
                             Toast.makeText(getApplicationContext(),"link: "+url_link,Toast.LENGTH_SHORT).show();

                        }
                    });*/
                     int count;
                     URL url = new URL(url_link);
                    URLConnection conexion = url.openConnection();
                    conexion.connect();
                    // this will be useful so that you can show a typical 0-100% progress bar
                    final int lenghtOfFile = conexion.getContentLength();

                    // Download the file
                    InputStream input = new BufferedInputStream(url.openStream());
                   // String name = "first.mp3";

                         File folder = new File(Environment.getExternalStoragePublicDirectory(
                                    Environment.DIRECTORY_MUSIC), "Vaishnav Songs");
                            if (!folder.mkdirs()) {
                                Log.e(DOWNLOAD_SERVICE, "Directory not created");
                            }

                         File filename = new File(folder,song_name);
                         OutputStream output = null;

                             output = new BufferedOutputStream(new FileOutputStream(filename));

                    byte data[] = new byte[1024];


                  mBuilder.setContentTitle(song_name);
                  int percentage;
                  id = song_index*10 + link_index;
                    while ((count = input.read(data)) != -1) {
                        total += count;
                        // publishing the progress....
                     //  publishProgress((int)(total*100/lenghtOfFile) );
                        percentage = (int)(total*100/lenghtOfFile) ;
                        if(percentage%5 ==0){
                           new Thread(new Runnable() {
                              public void run() {
                                 mBuilder.setProgress(100, (int)total*100/lenghtOfFile, false);
                                // Displays the progress bar for the first time.
                                mNotificationManager.notify(id, mBuilder.build());

                              }
                          }).start();
                        }



                        output.write(data, 0, count);
                    }

                    output.flush();
                    output.close();
                    input.close();
                    mBuilder.setContentIntent(resultPendingIntent);
                   new Thread(new Runnable() {
                      public void run() {
                         mBuilder.setContentText("Download complete")
                        // Removes the progress bar
                                .setProgress(100,100,false);
                        mNotificationManager.notify(id, mBuilder.build());

                      }
                  }).start();



                     }
                     catch (Exception e) {
                        error = e;
                         runOnUiThread(new Runnable() {
                            public void run() {
                                 Toast.makeText(getApplicationContext(),"Error: "+error,Toast.LENGTH_SHORT).show();

                            }
                        });
                    }





                 // Escape early if cancel() is called
               //  if (isCancelled()) break;


             return total;
         }

         protected void onProgressUpdate(Integer... progress) {
             progressBarStatus = progress[0];




         }

         protected void onPostExecute(Long result) {
            /* Toast.makeText(getApplicationContext(),"Downloaded "+result+" bytes",Toast.LENGTH_SHORT).show();*/
             Intent intent = getIntent();
             finish();
             //startActivity(intent);
     }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         selectedUri = Uri.parse(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_MUSIC) + "/"+"Vaishnav Songs"+"/");
          intent = new Intent(Intent.ACTION_VIEW);
          intent.setDataAndType(selectedUri, "resource/folder");
          resultPendingIntent =
                PendingIntent.getActivity(
                this,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT
            );

        song_name = (String) getIntent().getExtras().getString("songName");
        song_link = (String) getIntent().getExtras().getString("songLink");
        song_index = getIntent().getIntExtra("songIndex",0);
        link_index = getIntent().getIntExtra("position",0);
         Toast.makeText(getApplicationContext(),"Download started. Check progress in notification panel",Toast.LENGTH_SHORT).show();

        //new DownloadFilesTask().execute(song_link);
         DownloadFilesTask task = new DownloadFilesTask();
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,song_link);
        finish();
    }
}

1 个答案:

答案 0 :(得分:0)

发现此https://developer.android.com/reference/android/app/Notification.Builder.html
您可以尝试将setContentIntent(PendingIntent intent)添加到您的通知中,同时执行文件管理器打开操作。