歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android在線更新 遠程安裝程序

Android在線更新 遠程安裝程序

日期:2017/3/1 10:09:34   编辑:Linux編程

第一步:使用java.net的URLConnection對象來創建連接

第二步:通過InputStream將下載的文件寫入存儲卡內緩存

第三步:下載完畢之後,通過自定義的openFile()方法打開文件,判斷文件類型,若為APK,開始安裝

第四步:准備離開Installer程序的同時,通過自制的delFile()方法,刪除緩存內文件

  1. /**
  2. * 遠程下載安裝Android程序
  3. *
  4. * @ClassName InstallOnlineActivity
  5. * @author Jet
  6. * @date 2012-9-14
  7. */
  8. public class InstallOnlineActivity extends Activity {
  9. private TextView mTextView;
  10. private EditText mEditText;
  11. private Button mButton;
  12. private String currentFilePath = "";
  13. private String currentTempFilePath = "";
  14. private String strURL = "";
  15. private String fileEx = "";
  16. private String fileName = "";
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.installonline);
  21. mTextView = (TextView) findViewById(R.id.installonline_text1);
  22. mEditText = (EditText) findViewById(R.id.installonline_edittext1);
  23. mButton = (Button) findViewById(R.id.installonline_button1);
  24. mButton.setOnClickListener(new OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27. // 將文件下載到本地
  28. mTextView.setText("下載中...");
  29. strURL = mEditText.getText().toString();
  30. // 截取文件後綴
  31. fileEx = strURL.substring(strURL.lastIndexOf('.') + 1,
  32. strURL.length()).toLowerCase();
  33. // 截取文件名
  34. fileName = strURL.substring(strURL.lastIndexOf('/') + 1,
  35. strURL.lastIndexOf('.'));
  36. getFile(strURL);
  37. }
  38. });
  39. }
  40. private void getFile(final String strPath) {
  41. if (currentFilePath.equals(strPath)) {
  42. getDataSource(strPath);
  43. }
  44. currentFilePath = strPath;
  45. Runnable r = new Runnable() {
  46. @Override
  47. public void run() {
  48. getDataSource(strPath);
  49. }
  50. };
  51. new Thread(r).start();
  52. }
  53. private void getDataSource(String url) {
  54. if (!URLUtil.isNetworkUrl(url)) {
  55. mTextView.setText("請填寫正確的URL");
  56. } else {
  57. try {
  58. URL myUrl = new URL(url);
  59. // 取得連接
  60. URLConnection conn = myUrl.openConnection();
  61. // 連接
  62. conn.connect();
  63. // 獲得輸入流
  64. InputStream is = conn.getInputStream();
  65. if (is == null) {
  66. throw new RuntimeException("stream is null");
  67. }
  68. // 創建臨時文件
  69. File myTempFile = File.createTempFile(fileName, "." + fileEx);
  70. // 取得臨時文件存放路徑
  71. currentTempFilePath = myTempFile.getAbsolutePath();
  72. FileOutputStream fos = new FileOutputStream(myTempFile);
  73. byte[] buf = new byte[128];
  74. do {
  75. // 返回現在所讀緩沖區的大小
  76. int numread = is.read(buf);
  77. if (numread <= 0) {
  78. break;
  79. }
  80. fos.write(buf, 0, numread);
  81. } while (true);
  82. // 打開文件進行安裝
  83. openFile(myTempFile);
  84. is.close();
  85. } catch (MalformedURLException e) {
  86. e.printStackTrace();
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. }
  92. private void openFile(File file) {
  93. Intent intent = new Intent();
  94. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  95. intent.setAction(android.content.Intent.ACTION_VIEW);
  96. String type = getMimeType(file);
  97. intent.setDataAndType(Uri.fromFile(file), type);
  98. startActivity(intent);
  99. }
  100. private String getMimeType(File file) {
  101. String type = "";
  102. String fname = file.getName();
  103. // 獲得擴展名
  104. String end = fname
  105. .substring(fname.lastIndexOf('.') + 1, fname.length())
  106. .toLowerCase();
  107. // 按擴展名的類型決定MimeType
  108. if ("m4a".equals(end) || "mp3".equals(end) || "mid".equals(end)
  109. || "xmf".equals(end) || "ogg".equals(end) || "wav".equals(end)) {
  110. type = "audio";
  111. } else if ("3gp".equals(end) || "mp4".equals(end)) {
  112. type = "video";
  113. } else if ("jpg".equals(end) || "gif".equals(end) || "png".equals(end)
  114. || "jpeg".equals(end) || "bmp".equals(end)) {
  115. type = "image";
  116. } else if ("apk".equals(end)) {
  117. type = "application/vnd.android.package-archive";
  118. } else {
  119. type = "*";
  120. }
  121. if ("apk".equals(end)) {
  122. } else {
  123. type += "/*";
  124. }
  125. return type;
  126. }
  127. private void delFile(String fileName){
  128. File file = new File(fileName);
  129. if(file.exists()){
  130. file.delete();
  131. }
  132. }
  133. @Override
  134. protected void onPause() {
  135. mTextView = (TextView) findViewById(R.id.installonline_text1);
  136. mTextView.setText("下載成功");
  137. super.onPause();
  138. }
  139. @Override
  140. protected void onResume() {
  141. //刪除臨時文件
  142. delFile(currentTempFilePath);
  143. super.onResume();
  144. }
  145. }
Copyright © Linux教程網 All Rights Reserved