2017-06-21 110 views
1

我正在从URL下载文件,并且我想在通知下载完成时打开我的文件管理器。这样用户可以点击通知并从文件管理器打开文件。当我点击我的通知时,它什么都不做。我是一个Android的初学者,所以没有那么多的知识,任何人都可以帮助我解决如何做到这一点。通知打开文件管理器

这是我的代码。

public class DownloadFileFromURL extends AsyncTask<String, String, String> { 

private static final String LOG_TAG = "Directory"; 
Context context; 
ProgressDialog pDialog; 
Activity activity; 
NotificationManager mNotifyManager; 
Builder mBuilder; 
int id=1; 
String fileName; 

public DownloadFileFromURL(Context context){ 
    this.context = context; 
} 

/** 
* Before starting background thread 
* */ 
@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 

    ((Activity)context).runOnUiThread(new Runnable() { 
     public void run() { 
      pDialog = new ProgressDialog(context); 
      pDialog.setMessage("Downloading..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
      mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
       mBuilder = new android.support.v7.app.NotificationCompat.Builder(context); 
       mBuilder.setContentTitle("Assignment") 
         .setContentText("Download in progress").setSmallIcon(R.drawable.download); 
       mBuilder.setProgress(0, 0, false); 
       mNotifyManager.notify(id, mBuilder.build()); 

     } 
    }); 

} 
@Override 
protected void onProgressUpdate(String... values) { 
    mBuilder.setProgress(100, Integer.parseInt(values[0]), false); 
    mNotifyManager.notify(id, mBuilder.build()); 
    super.onProgressUpdate(values); 
} 
/** 
* Downloading file in background thread 
* */ 

@Override 
protected String doInBackground(String... f_url) { 
    int count; 
    try { 
     String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(); 
     File myDir = new File(root + "/saved_assignments"); 

     URL url = new URL(f_url[0]); 
     URLConnection conection = url.openConnection(); 
     conection.connect(); 

     String raw = conection.getHeaderField("Content-Disposition"); 

     fileName = raw.replaceFirst("(?i)^.*filename=\"([^\"]+)\".*$", "$1"); 
     if(fileName.contains("=")){ 
      int counter=0;String str=""; 
      for(int i=0; i<fileName.length();i++){ 
       if(fileName.charAt(i)=='='){ 
        counter = 1; 
       }else if(counter==1){ 
        str =str + fileName.charAt(i); 
       } 
      } 
      fileName = str ; 
     } 

     String PATH = Environment.getExternalStorageDirectory().toString()+ "/saved_assignments/"; 
     File folder = new File(PATH); 
     if(!folder.exists()){ 
      folder.mkdir();//If there is no folder it will be created. 
     } 

     String file = PATH+fileName; 
     File fileNew = new File(file); 
     try { 
      fileNew.createNewFile(); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "Directory not created"); 

     } 

     // input stream to read file - with 8k buffer 
     InputStream input = new BufferedInputStream(url.openStream()); 

     // Output stream to write file 

     OutputStream output = new FileOutputStream(fileNew); 
     byte data[] = new byte[1024]; 

     long total = 0; 
     while ((count = input.read(data)) != -1) { 
      total += count; 

      // writing data to file 
      output.write(data, 0, count); 

     } 

     // flushing output 
     output.flush(); 

     // closing streams 
     output.close(); 
     input.close(); 

    } catch (Exception e) { 
     Log.e("Error: ", e.getMessage()); 
    } 

    return null; 
} 

/** 
* After completing background task 
* **/ 
@Override 
protected void onPostExecute(String file_url) { 
    Toast.makeText(context,"Assignment saved in downloads",Toast.LENGTH_SHORT).show(); 
    mBuilder.setContentText("Download complete"); 
    // Removes the progress bar 
    mBuilder.setProgress(0, 0, false); 
    mNotifyManager.notify(id, mBuilder.build()); 
    pDialog.hide(); 
} 
} 
+0

参考https://stackoverflow.com/questions/6781306/notification-bar-when-clicked-how-to-open-file-explorer-with-specified -locati – sasikumar

+0

我以前曾试过这个解决方案 –

回答

0

试试这个:

private NotificationManager mNotifyManager; 
private NotificationCompat.Builder mBuilder; 
Notification noti; 
int nid = 1; 
String path; 
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_VIEW); 
File file = new File("Your File PATH"); // 
intent.setDataAndType(Uri.fromFile(file), "*/*"); // 
PendingIntent pIntent = PendingIntent.getActivity(activity.this, 0, intent, 0); 
mBuilder = new NotificationCompat.Builder(activity); 
mBuilder.setContentTitle("Download") 
     .setContentText("Download in progress") 
     .setSmallIcon(R.mipmap.alogo) 
     .setContentIntent(pIntent); 
mNotifyManager.notify(nid, mBuilder.build());