NotificationDownLoad 通知栏显示下载进度

关键点:

创建下载路径,文件夹格式,Intent 的调用

代码:

//带有通知栏的进度条
public class MainActivity extends Activity {


ImageView imageView;
private String downLoadAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.imageView);
}

public void downLoadApp(View button) {
// 判断网络
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {

DownloadAppTask task = new DownloadAppTask(this,imageView);
downLoadAddress="http://apkc.mumayi.com/2014/12/17/88/888923/zuozuorili_V4.2.1_mumayi_7a33c.apk";
//address="http://yunding.taidu8.com/data/attachment/forum/201409/09/165513mgjzg8ieedgouler.jpg";
task.execute(downLoadAddress);


} else {
Toast.makeText(this, "网络不可用...", Toast.LENGTH_SHORT).show();
}


}


 
}

//、

@SuppressLint("NewApi")
public class DownloadAppTask extends AsyncTask<String, Integer, Boolean> {


Context context;
NotificationManager nManager;
Builder builder;
private int RESULT_OK = 200;
InputStream is;
OutputStream os;
ImageView imageView;
Bitmap bitmap;
private int size = 0;
private int max;
private int PROGRESS_ID = 1;


public DownloadAppTask(Context context, ImageView imageView) {
// TODO Auto-generated constructor stub
this.context = context;
this.imageView = imageView;
}


@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();


nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
builder = new Notification.Builder(context);
builder.setContentText("apk正在下载...").setContentTitle("apk下载").setSmallIcon(R.drawable.ic_launcher);


}


@Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
try {
return downloadUrl(params[0]);
} catch (Exception e) {
return false;
}


}


private Boolean downloadUrl(String urlString) throws IOException {


URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10 * 1000);
conn.setConnectTimeout(15 * 1000);
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.connect();
int code = conn.getResponseCode();
if (code == RESULT_OK) {
is = conn.getInputStream();
// 获得网络图片
//bitmap = BitmapFactory.decodeStream(is);
File file = new File(Environment.getExternalStorageDirectory() + "/download/apk");


if (!file.exists()) {
file.mkdirs();
}
final File f = new File(file.getAbsolutePath(), "test.apk");


os = new FileOutputStream(f);
// 最经典的边读编写模式
byte[] buffer = new byte[1024];
max = conn.getContentLength();


new Thread(new Runnable() {


@Override
public void run() {


while (max > size) {
try {
publishProgress(size);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}


publishProgress(max);
// 文件下载完成,安装app
installApp(f);


}
}).start();
int len;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
size += len;
}


os.flush();
os.close();
is.close();
conn = null;
file = null;


return true;
}


return false;
}


/**
* 文件下载完成,安装App
* */
protected void installApp(File f) {


Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(f),"application/vnd.android.package-archive");

context.startActivity(intent);
 
}


@SuppressLint("NewApi")
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
if (max <= values[0]) {
builder.setContentText("apk下载完成");
builder.setProgress(0, 0, false);
} else {
builder.setProgress(max, values[0], false);


}
nManager.notify(PROGRESS_ID, builder.build());


}


@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);


//imageView.setImageBitmap(bitmap);
 
}


}

//其中显示网络图片的,太简单被干掉了,换成下载apk ,方便观看效果


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值