关键点:
创建下载路径,文件夹格式,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 ,方便观看效果