当前位置: 编程技术>移动开发
本页文章导读:
▪异步种下载 带进度条 异步类下载 带进度条
package com.shamusoft.asynctask;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.........
▪ 动态设定界面式样 动态设定界面样式
//通过执行下面的代码可以动态设定程序的状态栏和导航栏的样式
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
self.navigationController.navigationBar.barStyle =.........
▪ Iterator 跟 Iterable 接口的区别 Iterator 和 Iterable 接口的区别
为什么一定要实现Iterable接口,为什么不直接实现Iterator接口呢?
看一下JDK中的集合类,比如List一族或者Set一族,都是实现了Iterable接口,.........
[1]异步种下载 带进度条
来源: 互联网 发布时间: 2014-02-18
异步类下载 带进度条
package com.shamusoft.asynctask;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
String URL = "http://60.28.186.211/lyn/blue.zip";
TextView tv;
TextView tv2;
ProgressBar pb;
Handler _handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.tv);
tv2 = (TextView) this.findViewById(R.id.tv2);
pb = (ProgressBar) this.findViewById(R.id.firstBar);
_handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
tv2.setText("服务器异常");
}};
Masynctask task = new Masynctask();
task.execute(URL);
}
@SuppressWarnings("rawtypes")
public class Masynctask extends AsyncTask {
@Override
protected Object doInBackground(Object... params) {
Log.i("XX", "doInBackground");
try {
downLoad(URL);
} catch (Exception e) {
_handler.sendEmptyMessage(0);
// throw new RuntimeException();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// 任务启动,可以在这里显示一个对话框,这里简单处理
pb.setVisibility(0);
tv.setText("开始下载");
Log.i("XX", "onPreExecute");
}
@Override
protected void onPostExecute(Object result) {
tv.setText("下载完毕");
Log.i("XX", "onPostExecute");
}
@Override
protected void onProgressUpdate(Object... values) {
// 更新进度
pb.setProgress((Integer) values[0]);
tv2.setText("下载进度:" + values[0] + "%");
}
@SuppressWarnings("unchecked")
public void downLoad(String url) throws ClientProtocolException, IOException {
HttpClient client = getHttpClient();
HttpGet httpGet = new HttpGet(url);
int pro = 0;
HttpResponse httpResponse = client.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
int code = httpResponse.getStatusLine().getStatusCode();
Log.i("XX","code:"+code);
float length = entity.getContentLength();
InputStream is = entity.getContent();
if (is != null) {
File file = new File(
android.os.Environment
.getExternalStorageDirectory()
+ "/blue.zip");
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
byte[] buf = new byte[128];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
count += ch;
if (length > 0) {
pro = (int) (count * 100 / length);
publishProgress(pro);
}
out.write(ch);
}
is.close();
out.close();
}
}
private static final int REQUEST_TIMEOUT = 10 * 1000;// 设置请求超时10秒钟
private static final int SO_TIMEOUT = 10 * 1000; // 设置等待数据超时时间10秒钟
public HttpClient getHttpClient() {
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
REQUEST_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpParams);
return client;
}
}
}
package com.shamusoft.asynctask;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
String URL = "http://60.28.186.211/lyn/blue.zip";
TextView tv;
TextView tv2;
ProgressBar pb;
Handler _handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.tv);
tv2 = (TextView) this.findViewById(R.id.tv2);
pb = (ProgressBar) this.findViewById(R.id.firstBar);
_handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
tv2.setText("服务器异常");
}};
Masynctask task = new Masynctask();
task.execute(URL);
}
@SuppressWarnings("rawtypes")
public class Masynctask extends AsyncTask {
@Override
protected Object doInBackground(Object... params) {
Log.i("XX", "doInBackground");
try {
downLoad(URL);
} catch (Exception e) {
_handler.sendEmptyMessage(0);
// throw new RuntimeException();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// 任务启动,可以在这里显示一个对话框,这里简单处理
pb.setVisibility(0);
tv.setText("开始下载");
Log.i("XX", "onPreExecute");
}
@Override
protected void onPostExecute(Object result) {
tv.setText("下载完毕");
Log.i("XX", "onPostExecute");
}
@Override
protected void onProgressUpdate(Object... values) {
// 更新进度
pb.setProgress((Integer) values[0]);
tv2.setText("下载进度:" + values[0] + "%");
}
@SuppressWarnings("unchecked")
public void downLoad(String url) throws ClientProtocolException, IOException {
HttpClient client = getHttpClient();
HttpGet httpGet = new HttpGet(url);
int pro = 0;
HttpResponse httpResponse = client.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
int code = httpResponse.getStatusLine().getStatusCode();
Log.i("XX","code:"+code);
float length = entity.getContentLength();
InputStream is = entity.getContent();
if (is != null) {
File file = new File(
android.os.Environment
.getExternalStorageDirectory()
+ "/blue.zip");
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
byte[] buf = new byte[128];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
count += ch;
if (length > 0) {
pro = (int) (count * 100 / length);
publishProgress(pro);
}
out.write(ch);
}
is.close();
out.close();
}
}
private static final int REQUEST_TIMEOUT = 10 * 1000;// 设置请求超时10秒钟
private static final int SO_TIMEOUT = 10 * 1000; // 设置等待数据超时时间10秒钟
public HttpClient getHttpClient() {
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
REQUEST_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpParams);
return client;
}
}
}
[2] 动态设定界面式样
来源: 互联网 发布时间: 2014-02-18
动态设定界面样式
//通过执行下面的代码可以动态设定程序的状态栏和导航栏的样式 [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent; self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[3] Iterator 跟 Iterable 接口的区别
来源: 互联网 发布时间: 2014-02-18
Iterator 和 Iterable 接口的区别
为什么一定要实现Iterable接口,为什么不直接实现Iterator接口呢?
看一下JDK中的集合类,比如List一族或者Set一族,都是实现了Iterable接口,但并不直接实现Iterator接口。
仔细想一下这么做是有道理的。
因为Iterator接口的核心方法next()或者hasNext() 是依赖于迭代器的当前迭代位置的。
如果Collection直接实现Iterator接口,势必导致集合对象中包含当前迭代位置的数据(指针)。
当集合在不同方法间被传递时,由于当前迭代位置不可预置,那么next()方法的结果会变成不可预知。
除非再为Iterator接口添加一个reset()方法,用来重置当前迭代位置。
但即时这样,Collection也只能同时存在一个当前迭代位置。
而Iterable则不然,每次调用都会返回一个从头开始计数的迭代器。
多个迭代器是互不干扰的。
最新技术文章: