安装:

1
2
3
4
5
String str = "/CanavaCancel.apk";
String fileName = Environment.getExternalStorageDirectory() + str;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);

卸载:

1
2
3
Uri packageURI = Uri.parse("package:com.demo.CanavaCancel");  
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);  
startActivity(uninstallIntent);
Environment拥有一些可以获取环境变量的方法 
package:com.demo.CanavaCancel 这个形式是 package:程序完整的路径 (包名+程序名).

//下载apk程序代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
protected File downLoadFile(String httpUrl) {
                // TODO Auto-generated method stub
                final String fileName = "updata.apk";
                File tmpFile = new File("/sdcard/update");
                if (!tmpFile.exists()) {
                        tmpFile.mkdir();
                }
                final File file = new File("/sdcard/update/" + fileName);
 
                try {
                        URL url = new URL(httpUrl);
                        try {
                                HttpURLConnection conn = (HttpURLConnection) url
                                                .openConnection();
                                InputStream is = conn.getInputStream();
                                FileOutputStream fos = new FileOutputStream(file);
                                byte[] buf = new byte[256];
                                conn.connect();
                                double count = 0;
                                if (conn.getResponseCode() >= 400) {
                                        Toast.makeText(Main.this"连接超时", Toast.LENGTH_SHORT)
                                                        .show();
                                else {
                                        while (count <= 100) {
                                                if (is != null) {
                                                        int numRead = is.read(buf);
                                                        if (numRead <= 0) {
                                                                break;
                                                        else {
                                                                fos.write(buf, 0, numRead);
                                                        }
 
                                                else {
                                                        break;
                                                }
 
                                        }
                                }
 
                                conn.disconnect();
                                fos.close();
                                is.close();
                        catch (IOException e) {
                                // TODO Auto-generated catch block
 
                                e.printStackTrace();
                        }
                catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
 
                        e.printStackTrace();
                }
 
                return file;
        }
//打开APK程序代码
 
private void openFile(File file) {
                // TODO Auto-generated method stub
                Log.e("OpenFile", file.getName());
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(android.content.Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file),
                                "application/vnd.android.package-archive");
                startActivity(intent);
        }

Android实现应用下载并自动安装apk包的更多相关文章

  1. 使用拷贝的方式(adb push) 绕过Android系统和adb install直接安装APK

    某些情况下定制的Android系统为了限制用户安装应用,例如电视盒子,车载中控等,通过修改代码屏蔽了正常安装应用的方式 本文探讨如何在 adb shell 具有读写data分区目录的权限前提下,通过a ...

  2. Monkey入门之如何在android虚拟机中安装apk包

    如果想把公司的产品apk包安装到android虚拟机中,应该进行如下操作: 1.首先将apk包放到C:\Program Files\Android\android-sdk\platform-tools ...

  3. 在linux中自动向设备中安装apk包

    环境:华为手机 linux centos64 为了锻炼自己,我把脚本文件和APK文件放到了不同的路径下. 需求:将虚拟机中的100个apk包安装到手机中. import os,time os.chdi ...

  4. 命令行创建Android应用,生成签名,对APK包签名并编译运行

    一.命令行创建Android应用 android create project -n HelloWorld -t android-22 -p HelloWorld1 -k org.crazyit.he ...

  5. 使用bat命令实现拖动快速安装APK包

    平时安装APK包,每次都要打命令adb install *********** 很繁琐,网上找到一个用BAT命令快速安装的方法   在桌面创建一个bat文件,输入: @echo off title i ...

  6. 下载更新文件后,调用安装器自动安装apk文件

    在更新操作时,下载新apk之后,往往需要自动安装新apk,用以下代码即可安装下载在本地的apk文件(apkFile) Intent intent = new Intent(); intent.setA ...

  7. android 版本更新适配8.0,解决8.0手机无法更新自动安装apk

    随着android 7.0的普及android 8.0的也逐渐流行起来,那么google对权限方面又有了新的修改.而且我发现在android8.0中除了一些bug,比如说:在小米6(Android 8 ...

  8. 下载apk安装包后,调用安装器自动安装apk(适配7.0)

    在更新操作时,下载新apk之后,往往需要自动安装新apk,用以下代码即可安装下载在本地的apk文件(apkFile) Intent intent = new Intent(); intent.setA ...

  9. Android 7.0系统代码调用安装apk时报错FileUriExposedException完美解决

    项目更新遇到问题   Android项目开发中经常遇到下载更新的需求,以前调用系统安装器执行安装操作代码如下: Intent intent = new Intent(); intent.setActi ...

随机推荐

  1. USACO Section 4.3 Buy low,Buy lower(LIS)

    第一眼看到题目,感觉水水的,不就是最长下降子序列嘛!然后写……就呵呵了..要判重,还要高精度……判重我是在计算中加入各种判断.这道题比看上去麻烦一点,但其实还好吧.. #include<cstd ...

  2. 机器学习算法实现(R&Python code)

    Machine Learning Algorithms Machine Learning Algorithms (Python and R) 明天考试,今天就来简单写写机器学习的算法 Types Su ...

  3. Listview 多个ViewHolder实现

    简单代码示例: package com.edaixi.adapter; import android.content.Context; import android.view.View; import ...

  4. delphi如何获得当前操作系统语言环境

    function GetWindowsLanguage: string; var WinLanguage: ..] of char; begin VerLanguageName(GetSystemDe ...

  5. mysql 主从实现

    主库:192.168.1.19 从库:192.168.1.20 开启db_test单库复制 常见问题 参考文档 主配置  以mysql root用户登录,用下面sql创建专门用于主从复制的mysql用 ...

  6. LINQ to SQL的CRUD操作

    创建数据对象模型 sqlmetal /code:"C:\MyProjects\VS2008\Data\LinqConsoleApp2\LinqConsoleApp2\northwnd.cs& ...

  7. Java实现HTML转PDF的总结

    Java实现HTML转PDF的几种方法—主要解决中文乱码问题 第一:同事在做HTML转PDF中遇到乱码问题 ********************************************** ...

  8. cpu亲和力总结taskset和setcpu及其他相关

    一:taskset -- 获取或指定进程运行的CPU. man taskset出现 CPU affinity is a scheduler property that "bonds" ...

  9. java程序错误类型及异常处理

    一.程序的错误类型 在程序设计中,无论规模是大是小,错误总是难免的.程序的设计很少有能够一次完成,没有错误的(不是指HelloWorld这样的程序,而是要实现一定的功能,具备一定实用价值的程序),在编 ...

  10. 第七届河南省赛H.Rectangles(lis)

    10396: H.Rectangles Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 229  Solved: 33 [Submit][Status] ...