Android更新下载进度条
2012-10-31 23:35
561 查看
下载文件会阻塞UI主线程,所以需要new一个新线程来执行下载操作,通过handler执行更新UI进度条操作。代码如下:
public class AndroidTest extends Activity { private static final String TAG = "AndroidTest"; private ProgressBar progressBar = null; private Button startButton = null; private EditText filenameText = null; private MyHandler handler = null; private Message message = null; private boolean flag = true; private int size = 1; private int hasRead = 0; private int len = 0; private byte buffer[] = new byte[1024*4]; private int index = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progressBar = (ProgressBar)findViewById(R.id.progress_horizontal); startButton = (Button)findViewById(R.id.mybutton); startButton.setOnClickListener(new ButtonClick()); filenameText = (EditText)findViewById(R.id.fileNameID); handler = new MyHandler(); } public boolean downloadFile(final String urlStr, final String filename) { new Thread(new Runnable(){ public void run() { try { URL url = new URL(urlStr); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); size = connection.getContentLength(); InputStream inputStream = connection.getInputStream(); OutputStream outputStream = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+filename); while((len=inputStream.read(buffer))!=-1){ outputStream.write(buffer); hasRead+=len; index = (int)(hasRead*100)/size; message = new Message(); message.what = 1; handler.sendMessage(message); Log.d(TAG, "index = " + index); System.out.println("has = "+hasRead+" size = "+size+" index = "+index); } inputStream.close(); outputStream.close(); } catch (Exception e) { flag = false; e.printStackTrace(); } } }).start(); return flag; } class ButtonClick implements OnClickListener { public void onClick(View v) { String url = filenameText.getText().toString(); String filename = url.substring(url.lastIndexOf('/') + 1); Log.d(TAG, "url = " + url); Log.d(TAG, "filename = " + filename); if(!downloadFile(url, filename)) { String rs = "下载失败 "; Toast.makeText(AndroidTest.this, rs, Toast.LENGTH_SHORT).show(); } } } class MyHandler extends Handler{ @Override public void handleMessage(Message msg) { if (msg.what == 1) { progressBar.setProgress(index); Log.d(TAG, "setProgress index:" + index); if (index >= 99) { String rs = "下载完成"; Toast.makeText(AndroidTest.this, rs, Toast.LENGTH_SHORT).show(); } } super.handleMessage(msg); } } }
相关文章推荐
- android之apk自动更新采用Notification通知提示并显示下载进度
- Android观察设计模式实现两个页面下载进度的同步更新
- android下载单个文件时时更新进度条
- Android提示版本更新+notification显示下载进度
- 安卓自动更新,静默更新,替换友盟更新可以在通知栏里显示更新下载进度,几行代码快速实现Android下载更新
- Android 通过蒲公英pgyer的接口 Service 实现带进度下载App 通知栏显示 在线更新 自动更新Demo
- Android中使用AsyncTask实现文件下载以及进度更新提示
- Android中使用Notification实现应用更新显示下载进度
- Android xutils 分析修改 下载进度更新频率
- Android更新下载进度条
- android中DownloadManager实现版本更新,监听下载进度实例
- Android 文件下载,断点续传,进度更新
- Android中使用AsyncTask实现下载文件动态更新进度条功能
- Android多任务下载,使用Notification显示、更新进度条
- Android 自动更新之状态栏下载状态和进度
- Android更新下载进度条
- Android 通过蒲公英pgyer的接口 Service 实现带进度下载App 通知栏显示 在线更新 自动更新Demo
- android之apk自动更新采用Notification通知提示并显示下载进度
- Android 自动更新之状态栏下载状态和进度
- Android服务器——使用TomCat实现软件的版本检测,升级,以及下载更新进度!