安装:

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. android中ScrollView和GridView/ListView共存时,ScrollView不在顶部的解决方法

    listView.setFocusable(false); gridView.setFocusable(false); 这个必须在代码中写,xml文件中设置不起作用 原文:http://stackov ...

  2. Decorators and Wrappers in Python

    python代码一贯以优雅,简洁著称,而有时侯反而会让人难以理解,比如说wrapper(或者说decorator),这种方式提高了代码的可重用性,使用起来更简洁方便. 举个例子,比如WebApp常用的 ...

  3. Day 1: How to install jedi/codeintel plugin for sublime on Linux

    Step 1, Install sublime3 Download sublime2/3 from http://www.sublimetext.com/ $tar -jxvf sublime_tex ...

  4. [Java]利用栈判断括号是否完整配对

    利用栈实现判断字符串中的括号是否都是配对的. 主要算法是依次读取字符串中的每一个字符,如果是左括号则将左括号压入栈中,如果是右括号则从栈中弹出最上面的字符,若两者不是同种括号或栈内已经没有字符就返回f ...

  5. IOS 特定于设备的开发:获取额外的设备信息

    sysctl()和sysctlbyname()允许获取系统信息.这些标准的UNIX函数用于询问操作系统有关硬件和OS的详细信息. 这些常量使你能够检查核心信息,比如系统的CPU频率,可用的内存量等.它 ...

  6. Gulp 从0开始

    http://www.w3ctech.com/topic/134  (该文章有很多错误) http://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97 ...

  7. sqlite 查询数据 不用回调

    int main( void ){    sqlite3 *db=NULL;    char *zErrMsg = 0;    int rc;    //打开数据库连接    rc = sqlite3 ...

  8. mysql 主从实现

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

  9. Google浏览器调试js

    1.进入source找到js位置 一般js文件就到js目录下找,tpl中的就到tpl中找. 我这次写在tpl中的,就到list中找,它就把list中的js单独显示出来了. 2.设置断点,进行排错.刷新 ...

  10. php知识点集合

    ---------------------------------------------------------           PHP知识大全 ------------------------ ...