请选择 进入手机版 | 继续访问电脑版

微嵌工业平板开发论坛

 找回密码
 立即注册
搜索
热搜: android wince6.0
查看: 1218|回复: 0

安卓在线下载并升级安装apk程序

[复制链接]

96

主题

28

回帖

2649

积分

金牌会员

Rank: 6Rank: 6

积分
2649
发表于 2022-3-4 08:49:30 | 显示全部楼层 |阅读模式

以下为例程代码:
MainActivity:
  1. package com.example.autoupdate;

  2. import androidx.appcompat.app.AppCompatActivity;

  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.os.StrictMode;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.webkit.URLUtil;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.TextView;

  15. import java.io.BufferedInputStream;
  16. import java.io.BufferedOutputStream;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import java.net.URLConnection;
  25. import java.util.zip.GZIPInputStream;
  26. import java.util.zip.ZipEntry;
  27. import java.util.zip.ZipInputStream;

  28. /**
  29. * 远程下载安装Android程序
  30. */
  31. public class MainActivity extends AppCompatActivity {
  32.     TextView mTextView;
  33.     EditText mEditText;
  34.     private Button mButton;
  35.     private String currentFilePath = "";
  36.     private String currentTempFilePath = "";
  37.     private String strURL = "";
  38.     private String fileEx = "";
  39.     private String fileName = "";

  40.     static String strAPKName = "";//保存每个zip的apk名称

  41.     @Override
  42.     protected void onCreate(Bundle savedInstanceState) {
  43.         super.onCreate(savedInstanceState);
  44.         setContentView(R.layout.activity_main);
  45.         mTextView = (TextView) findViewById(R.id.installonline_text1);
  46.         mEditText = (EditText) findViewById(R.id.installonline_edittext1);
  47.         mEditText.setText("http://www.wqlcd.net:8000/bbs/forum.php?mod=attachment&aid=MzM5fDhlODlmZWZmfDE2NDYyODY1OTJ8NXwxMTk%3D");
  48.         mButton = (Button) findViewById(R.id.installonline_button1);
  49.         mButton.setOnClickListener(new View.OnClickListener() {

  50.             @Override
  51.             public void onClick(View v) {
  52.                 // 将文件下载到本地
  53.                 mTextView.setText("下载中...");
  54.                 strURL = mEditText.getText().toString();
  55.                 // 截取文件后缀
  56.                 fileEx = strURL.substring(strURL.lastIndexOf('.') + 1,
  57.                         strURL.length()).toLowerCase();
  58.                 // 截取文件名
  59.                 fileName = strURL.substring(strURL.lastIndexOf('/') + 1,
  60.                         strURL.lastIndexOf('.'));
  61.                 getFile(strURL);
  62.             }

  63.         });
  64.     }

  65.     private void getFile(final String strPath) {
  66.         if (currentFilePath.equals(strPath)) {
  67.             getDataSource(strPath);
  68.         }
  69.         currentFilePath = strPath;
  70.         Runnable r = new Runnable() {

  71.             @Override
  72.             public void run() {
  73.                 getDataSource(strPath);
  74.             }
  75.         };
  76.         new Thread(r).start();
  77.     }

  78.     private void getDataSource(String url) {
  79.         if (!URLUtil.isNetworkUrl(url)) {
  80.             mTextView.setText("请填写正确的URL");
  81.         } else {
  82.             try {
  83.                 //网络不能在主线程
  84.                 StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
  85.                 StrictMode.setThreadPolicy(policy);

  86.                 URL myUrl = new URL(url);
  87.                 // 取得连接
  88.                 URLConnection conn = myUrl.openConnection();
  89.                 // 连接
  90.                 conn.connect();
  91.                 // 获得输入流
  92.                 InputStream is = conn.getInputStream();
  93.                 if (is == null) {
  94.                     throw new RuntimeException("stream is null");
  95.                 }
  96.                 // 创建临时文件
  97.                 File myTempFile = File.createTempFile(fileName, "." + fileEx);
  98.                 // 取得临时文件存放路径
  99.                 currentTempFilePath = myTempFile.getAbsolutePath();
  100.                 FileOutputStream fos = new FileOutputStream(myTempFile);
  101.                 byte[] buf = new byte[128];
  102.                 do {
  103.                     // 返回现在所读缓冲区的大小
  104.                     int numread = is.read(buf);
  105.                     if (numread <= 0) {
  106.                         break;
  107.                     }
  108.                     fos.write(buf, 0, numread);
  109.                 } while (true);

  110.                 //解压
  111.                 Unzip( currentTempFilePath, "/mnt/sdcard/Download/");
  112.                 File path1=new File("/mnt/sdcard/Download/" +strAPKName);
  113.                 // 打开文件进行安装
  114.                 openFile(path1);
  115.                 //openFile(myTempFile);
  116.                 is.close();
  117.                 //delFile("/mnt/sdcard/Download/" +strAPKName);
  118.             } catch (MalformedURLException e) {
  119.                 e.printStackTrace();
  120.             } catch (IOException e) {
  121.                 e.printStackTrace();
  122.             }
  123.         }
  124.     }

  125.     private void openFile(File file) {
  126.         Intent intent = new Intent();
  127.         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  128.         intent.setAction(android.content.Intent.ACTION_VIEW);
  129.         String type = getMimeType(file);
  130.         intent.setDataAndType(Uri.fromFile(file), type);
  131.         startActivity(intent);
  132.     }

  133.     private String getMimeType(File file) {
  134.         String type = "";
  135.         String fname = file.getName();
  136.         // 获得扩展名
  137.         String end = fname
  138.                 .substring(fname.lastIndexOf('.') + 1, fname.length())
  139.                 .toLowerCase();
  140.         // 按扩展名的类型决定MimeType
  141.         if ("m4a".equals(end) || "mp3".equals(end) || "mid".equals(end)
  142.                 || "xmf".equals(end) || "ogg".equals(end) || "wav".equals(end)) {
  143.             type = "audio";
  144.         } else if ("3gp".equals(end) || "mp4".equals(end)) {
  145.             type = "video";
  146.         } else if ("jpg".equals(end) || "gif".equals(end) || "png".equals(end)
  147.                 || "jpeg".equals(end) || "bmp".equals(end)) {
  148.             type = "image";
  149.         } else if ("apk".equals(end)) {
  150.             type = "application/vnd.android.package-archive";
  151.         } else if ("rar".equals(end) || "zip".equals(end)) {
  152.             type = "zip";
  153.         } else {
  154.             type = "*";
  155.         }
  156.         if ("apk".equals(end)) {

  157.         } else {
  158.             type += "/*";
  159.         }
  160.         return type;
  161.     }
  162.     private void delFile(String fileName){
  163.         File file = new File(fileName);
  164.         if(file.exists()){
  165.             file.delete();
  166.         }
  167.     }
  168.     @Override
  169.     protected void onPause() {
  170.         mTextView = (TextView) findViewById(R.id.installonline_text1);
  171.         mTextView.setText("下载成功");
  172.         super.onPause();
  173.     }

  174.     private static void Unzip(String zipFile, String targetDir) {
  175.             int BUFFER = 4096; //这里缓冲区我们使用4KB,
  176.             String strEntry; //保存每个zip的条目名称
  177.             try {
  178.                 BufferedOutputStream dest = null; //缓冲输出流
  179.                 FileInputStream fis = new FileInputStream(zipFile);
  180.                 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
  181.                 ZipEntry entry; //每个zip条目的实例
  182.                 while ((entry = zis.getNextEntry()) != null) {

  183.                     try {
  184.                         Log.i("Unzip: ","="+ entry);
  185.                         int count;
  186.                         byte data[] = new byte[BUFFER];
  187.                         strEntry = entry.getName();
  188.                         strAPKName = entry.getName();

  189.                         File entryFile = new File(targetDir + strEntry);
  190.                         File entryDir = new File(entryFile.getParent());
  191.                         if (!entryDir.exists()) {
  192.                             entryDir.mkdirs();
  193.                         }

  194.                         FileOutputStream fos = new FileOutputStream(entryFile);
  195.                         dest = new BufferedOutputStream(fos, BUFFER);
  196.                         while ((count = zis.read(data, 0, BUFFER)) != -1) {
  197.                             dest.write(data, 0, count);
  198.                         }
  199.                         dest.flush();
  200.                         dest.close();
  201.                     } catch (Exception ex) {
  202.                         ex.printStackTrace();
  203.                     }
  204.                 }
  205.                 zis.close();
  206.             } catch (Exception cwj) {
  207.                 cwj.printStackTrace();
  208.             }
  209.         }

  210. }
复制代码
activity_main.xml:界面文件请自行补全即可

AndroidManifest.xml:添加系统签名
  1. android:sharedUserId="android.uid.system"
复制代码




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|微嵌工业平板开发论坛

GMT+8, 2024-3-28 21:24 , Processed in 0.020183 second(s), 17 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表