80,351
社区成员
发帖
与我相关
我的任务
分享
/**
* 下载新程序
*/
public void appUpdate(Context context){
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGe("服务器地址"+"/update/XXX.apk");
HttpResponse response;
InputStream is = null;
FileOutputStream fileOutputStream = null;
try {
response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
is = entity.getContent();
long len = entity.getContentLength();
if(is!=null){
fileOutputStream = context.openFileOutput("XX.apk",
Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
byte[] buf = new byte[1024];
int ch = -1;
while((ch = is.read(buf)) != -1){
fileOutputStream.write(buf, 0, ch);
}
}
fileOutputStream.flush();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
switch (msg.what) {
case 1:
progressDialog.dismiss();
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(
"/data/data/包名/files/XXX.apk");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri,
"application/vnd.android.package-archive");
context.startActivity(intent);
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}