在主界面点击按钮下载,通知栏里出现下载进度。
1.主界面layout:
test.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击下载" />
</LinearLayout>
2.通知栏中显示进度的layout:
content_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="@+id/content_view_image"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/content_view_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0%"
android:textColor="#000000"
android:layout_toRightOf="@id/content_view_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
/>
<ProgressBar
android:id="@+id/content_view_progress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal"
android:max="100"
android:layout_below="@id/content_view_image"
android:layout_marginTop="4dp"
/>
</RelativeLayout>
3.主程序
test.java:
package com.example.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import com.example.androidgametest.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Toast;
public class test extends Activity {
private Button mBtnClick;
//通知栏进度条
private NotificationManager mNotificationManager=null;
private Notification mNotification;
private int fileSize;
int downLoadFileSize;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 定义一个Handler,用于处理下载线程与UI间通讯
switch (msg.what) {
case 1:
int result = downLoadFileSize * 100 / fileSize;
mNotification.contentView.setTextViewText(R.id.content_view_text1, result + "%");
mNotification.contentView.setProgressBar(R.id.content_view_progress, fileSize, downLoadFileSize, false);
mNotificationManager.notify(0, mNotification);
Log.e("size", "文件"+downLoadFileSize+":"+fileSize+":"+result);
break;
case 2:
Toast.makeText(test.this, "文件下载完成", 1).show();
break;
case -1:
String error = msg.getData().getString("error");
Toast.makeText(test.this, error, 1).show();
break;
default:
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
mBtnClick=(Button) findViewById(R.id.button1);
mBtnClick.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
notificationInit();
new Thread() {
public void run() {
try {
down_file("http://download.apk3.com/soft/cntvnews-v5.1.4.apk", Environment.getExternalStorageDirectory()+"/");
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
});
}
public void down_file(String url, String path) throws IOException {
// 下载函数
String filename = url.substring(url.lastIndexOf("/") + 1);
// 获取文件名
URL myURL = new URL(url);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
this.fileSize = conn.getContentLength();// 根据响应获取文件大小
if (this.fileSize <= 0)
throw new RuntimeException("无法获知文件大小 ");
if (is == null)
throw new RuntimeException("stream is null");
FileOutputStream fos = new FileOutputStream(path + filename);
// 把数据存入路径+文件名
byte buf[] = new byte[1024];
downLoadFileSize = 0;
sendMsg(0);
int numread=0;
while((numread = is.read(buf))!=-1){
fos.write(buf, 0, numread);
downLoadFileSize += numread;
sendMsg(1);// 更新进度条
}
sendMsg(2);// 通知下载完成
try {
fos.close();
is.close();
} catch (Exception ex) {
Log.e("tag", "error: " + ex.getMessage(), ex);
}
}
private void sendMsg(int flag) {
Message msg = new Message();
msg.what = flag;
handler.sendMessage(msg);
}
private void notificationInit(){
//通知栏内显示下载进度条
Intent intent=new Intent(this,test.class);//点击进度条,进入程序
PendingIntent pIntent=PendingIntent.getActivity(this, 0, intent, 0);
mNotificationManager=(NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
mNotification=new Notification();
mNotification.icon=R.drawable.ic_launcher;
mNotification.tickerText="开始下载";
mNotification.contentView=new RemoteViews(getPackageName(),R.layout.content_view);//通知栏中进度布局
mNotification.contentIntent=pIntent;
// mNotificationManager.notify(0,mNotification);
}
}