[Android实例] android 应用客户端自动升级

来源:互联网 发布:淘宝一航日本笔假货 编辑:程序博客网 时间:2024/05/11 14:48
[Android实例] android 应用客户端自动升级1.import java.io.File; 2.3.import java.io.FileOutputStream; 4.5.import java.io.IOException; 6.7.import java.io.InputStream; 8.9.import org.apache.http.HttpEntity; 10.import org.apache.http.HttpResponse; 11.import org.apache.http.client.ClientProtocolException; 12.import org.apache.http.client.HttpClient; 13.import org.apache.http.client.methods.HttpGet; 14.import org.apache.http.impl.client.DefaultHttpClient; 15. 16.import android.app.AlertDialog; 17.import android.app.Dialog; 18.import android.app.ProgressDialog; 19.import android.content.DialogInterface; 20.import android.content.Intent; 21.import android.net.Uri; 22.import android.os.Bundle; 23.import android.os.Environment; 24.import android.os.Handler; 25. 26.public class Update extends BaseActivity { 27. public ProgressDialog pBar; 28. private Handler handler = new Handler(); 29. 30. @Override 31. protected void onCreate(Bundle savedInstanceState) { 32. super.onCreate(savedInstanceState); 33. setContentView(R.layout.update); 34. Dialog dialog = new AlertDialog.Builder(Update.this).setTitle("系统更新") 35. .setMessage("发现新版本,请更新!")// 设置内容 36. .setPositiveButton("确定",// 设置确定按钮 37. new DialogInterface.OnClickListener() { 38. 39. @Override 40. public void onClick(DialogInterface dialog, 41. int which) { 42. pBar = new ProgressDialog(Update.this); 43. pBar.setTitle("正在下载"); 44. pBar.setMessage("请稍候..."); 45. pBar 46. .setProgressStyle(ProgressDialog.STYLE_SPINNER); 47. downFile("http://url:8765/OA.apk"); 48. 49. 50. } 51. 52. }).setNegativeButton("取消", 53. new DialogInterface.OnClickListener() { 54. public void onClick(DialogInterface dialog, 55. int whichButton) { 56. // 点击"取消"按钮之后退出程序 57. 58. } 59. }).create();// 创建 60. // 显示对话框 61. dialog.show(); 62. 63. } 64. 65. void downFile(final String url) { 66. pBar.show(); 67. new Thread() { 68. public void run() { 69. HttpClient client = new DefaultHttpClient(); 70. // params[0]代表连接的url 71. HttpGet get = new HttpGet(url); 72. HttpResponse response; 73. try { 74. response = client.execute(get); 75. HttpEntity entity = response.getEntity(); 76. long length = entity.getContentLength(); 77. InputStream is = entity.getContent(); 78. FileOutputStream fileOutputStream = null; 79. if (is != null) { 80. 81. File file = new File(Environment 82. .getExternalStorageDirectory(), "OA.apk"); 83. fileOutputStream = new FileOutputStream(file); 84. 85. byte[] buf = new byte[1024]; 86. int ch = -1; 87. int count = 0; 88. while ((ch = is.read(buf)) != -1) { 89. // baos.write(buf, 0, ch); 90. fileOutputStream.write(buf, 0, ch); 91. count += ch; 92. if (length > 0) { 93. 94. } 95. 96. } 97. 98. } 99. fileOutputStream.flush(); 100. if (fileOutputStream != null) { 101. fileOutputStream.close(); 102. } 103. down(); 104. } catch (ClientProtocolException e) { 105. // TODO Auto-generated catch block 106. e.printStackTrace(); 107. } catch (IOException e) { 108. // TODO Auto-generated catch block 109. e.printStackTrace(); 110. } 111. } 112. 113. }.start(); 114. 115. } 116. 117. void down() { 118. handler.post(new Runnable() { 119. public void run() { 120. pBar.cancel(); 121. update(); 122. } 123. }); 124. 125. } 126. 127. void update() { 128. 129. Intent intent = new Intent(Intent.ACTION_VIEW); 130. intent.setDataAndType(Uri.fromFile(new File("/sdcard/OA.apk")), 131. "application/vnd.android.package-archive"); 132. startActivity(intent); 133. } 134. 135.}

原创粉丝点击