在下面的代码中,当“下载完成”和“用户点击通知”时,它会直接进入“文件管理器”并打开“我的应用程序”#34;夹
在文件夹图像文件中,有PDF文件以及excel,word或zip文件。
文件夹路径为:/ storage / emulated / 0 / My Application
我的代码是:
ProgressDialog pDialog; public static final int progress_bar_type = 0;
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
build.setProgress(100, 0, false);
mNotifyManager.notify(ID, build.build());
pDialog = new ProgressDialog(activity);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
String NoticeUrl = "http://" + DOMAIN +"//NoticeBoard/";
System.out.println("============ Notice : =============" + NoticeUrl);
Log.d("file name exec", f_url[0] + "");
URL url = new URL( NoticeUrl
+ Uri.encode(f_url[0].trim()));
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),
8192);
direct = new File(Environment.getExternalStorageDirectory()+"/My Application");
if(!direct.exists()) {
if(direct.mkdir()); //directory is created;
}
// Output stream to write file
OutputStream output = new FileOutputStream(
direct + "/" + f_url[0].trim());
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
// Displaying downloaded image into image view
// Reading image path from sdcard
FilePath = direct + "/" + f_url[0].trim();
// setting downloaded into image view
//my_image.setImageDrawable(Drawable.createFromPath(imagePath));
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
int i;
for (i = 0; i <= 100; i += 5) {
// Sets the progress indicator completion percentage
publishProgress(String.valueOf(Math.min(i, 100)));
try {
// Sleep for 5 seconds
Thread.sleep(2 * 1000);
} catch (InterruptedException e) {
Log.d("Failure", "sleeping failure");
}
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
build.setProgress(100, Integer.parseInt(progress[0]), false);
mNotifyManager.notify(ID, build.build());
super.onProgressUpdate(progress);
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String f_url) {
// dismiss the dialog after the file was downloaded
pDialog.dismiss();
Toast.makeText(activity, "File Downloaded at : - " + FilePath , 4600).show();
System.out.println("==================" + FilePath);
Toast.makeText(activity, "Download Completed", 200).show();
// build.setSmallIcon(android.R.drawable.ic_dialog_alert);
/*File file = new File(FilePath);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "");
PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplication(), 0, intent, 0);
build.setContentIntent(pendingIntent);
build.setLargeIcon(BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_launcher));*/
build.setContentText("Download complete");
build.setProgress(0, 0, false);
mNotifyManager.notify(ID, build.build());
//Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//Uri uri = Uri.parse(FilePath); // a directory
//intent.setDataAndType(uri, "*/*");
//activity.startActivity(Intent.createChooser(intent, "Open folder"));
}
}
答案 0 :(得分:0)
首先,您需要设置一个活动,该活动将在notification
点击时打开,然后您需要ListView
在ListView
中显示所需的文件。
以下代码可能会对您有所帮助。它可以帮助您查看listview和
中的文件public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myList = new ArrayList<String>();
String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File( root_sd + "/external_sd" ) ;
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myList ));
}
根据您要执行的操作实现onClick方法。
我希望它有所帮助。