package com.example.progressnotification;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.util.EntityUtils;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView image;
private int sum;
private boolean isSum = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
}
public void btnClick(View view) {
new DownloadImage()
.execute("http://sony.it168.com/data/attachment/forum/201410/20/222208nct678ofnorcwclf.jpg");
}
class DownloadImage extends AsyncTask<String, Integer, Bitmap> {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
MainActivity.this).setSmallIcon(R.drawable.ic_launcher).setContentTitle("下载图片").setContentText("请稍后");
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int totalNum = 0;
private byte[] imageByteArray;
@Override
protected Bitmap doInBackground(String... params) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(params[0]);
ByteArrayOutputStream baos = null;
try {
HttpResponse res = client.execute(get);
if (res.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = res.getEntity();
InputStream is = entity.getContent();
// 获得最大值
sum = (int) entity.getContentLength();
publishProgress(sum);
baos = new ByteArrayOutputStream();
byte[] b = new byte[100];
int a = 0;
int total = 0;
while ((a = is.read(b)) != -1) {
total += a;
// 更新当前进度
publishProgress(total);
baos.write(b, 0, a);
}
Bitmap bitmap = BitmapFactory.decodeByteArray(
baos.toByteArray(), 0, baos.toByteArray().length);
return bitmap;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
image.setImageBitmap(result);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (isSum) {
totalNum = values[0];
isSum = false;
} else {
builder.setProgress(totalNum,values[0], false);
manager.notify(1, builder.build());
if (values[0] == totalNum) {
builder.setProgress(0, 0, false);
builder.setContentText("下载完毕");
manager.notify(1, builder.build());
}
}
}
}
}