zoukankan      html  css  js  c++  java
  • Android apk下载 安装 卸载 打开

    今天来介绍一下Android apk安装、卸载、打开

    安装
    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);
    卸载:
    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程序代码
    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);
            }

  • 相关阅读:
    设计一个圆柱体类,计算表面积及体积。建立一个半径为3、高为3.5的圆柱体,输出其表面积及体积
    写一个方法完成如下功能,判断从文本框textbox1输入的一个字符,如果是数字则求该数字的阶乘,如果是小写字条,则转换为大写,大写字符不变,结果在文本框textbox2中显示
    写一方法用来计算1+2+3+...n,其中n作为参数输入,返回值可以由方法名返回,也可以由参数返回
    winform控件记录
    写4个同名方法,实现两个整数、两个实数,一个实数一个整数,一个整数一个实数之间的求和。在主调函数中调用这4个方法计算相关的值。(方法的重载)
    写一方法计算实现任意个整数之和.在主调函数中调用该函数,实现任意个数之和。(使用params参数)
    在主函数中提示用户输入用户名和密码。另写一方法来判断用户输入是否正确。该方法分别返回一个bool类型的登录结果和和一个string类型的登录信息。如登录成功,返回true及“登录成功”,若登录失败则返回false及“用户名错误”或“密码错误”(使用out参数)
    Linux下使用Kickstart自动化安装平台架构
    Day10 多线程理论 开启线程
    关闭ipv6的方法
  • 原文地址:https://www.cnblogs.com/xieyuan/p/3787522.html
Copyright © 2011-2022 走看看