Android:如何使用进度条下载文件和显示通知?

时间:2011-10-11 16:20:35

标签: android asynchronous notifications progress-bar

我有一个活动,可以在点击按钮时从网上下载文件。文件的下载是通过内部AsyncTask完成的。下载工作,我甚至可以使用AsyncTask在Activitys layout.xml中显示,更新和隐藏进度条小部件。我还可以显示来自此AsyncTask的通知。但是,我不能从AsyncTask更新通知中的进度条。处理完第一个更新后,我会立即关闭Force。如果需要,我会提供我的代码!

感谢您的时间

InvocationTargetException.<init>(Throwable) line: 50    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not              available [native method] 
Method.invoke(Object, Object...) line: 507  
ZygoteInit$MethodAndArgsCaller.run() line: 845  
ZygoteInit.main(String[]) line: 603 
NativeStart.main(String[]) line: not available [native method]  

这是抛到这一行

myContentView.setProgressBar(R.id.status_progress, 100, Integer.parseInt(progress[0]), false);

这是我的代码

protected String doInBackground(String... aurl) {
int count;

try {

URL url = new URL(aurl[0]);
String path = url.getPath();
String filename = new File(path).getName();
Environment.getExternalStorageDirectory();
File tm = new File("/sdcard/Transform Mii/");
tm.mkdirs();
File out = new File(tm, filename);
URLConnection conexion = url.openConnection();
conexion.connect();

int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(out);

byte data[] = new byte[1024];

long total = 0;

while ((count = input.read(data)) != -1) {
    total += count;
    publishProgress(""+(int)((total*100)/lenghtOfFile));
    output.write(data, 0, count);
}

output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;

}
protected void onProgressUpdate(String... progress) {
progressbar.setProgress(Integer.parseInt(progress[0]));
myContentView.setProgressBar(R.id.status_progress, 100, Integer.parseInt(progress[0]), false);
notificationManager.notify(NOTIF_ID, notification);
}

progressbar是活动的layout.xml中的栏,通常会更新

myContentView进度条是通知中的栏

0 个答案:

没有答案