其实很简单,只需要几行代码就好了,首先要到服务器下载apk,然后才能安装,当然不是傻子应该都知道,我这里用到的是Httputils去下载,

这里需要一些权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>

  

直接贴代码

package com.exmple.httpxutil;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar; import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack; public class MainActivity extends Activity {
HttpHandler<File> h;
private ProgressBar pa; @SuppressLint("SdCardPath")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
pa = (ProgressBar) findViewById(R.id.pro);
Button more = (Button) findViewById(R.id.more);
getCode();
more.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent ints = new Intent(MainActivity.this, NewActivity.class);
startActivity(ints); }
});
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { HttpUtils http = new HttpUtils();
final String path = Environment.getExternalStorageDirectory()
.getPath();
System.out.println(path); h = http.download(
"http://101.200.142.201:8080/tqyb/baidumap.apk", path
+ "/badumap.apk", true, true,
new RequestCallBack<File>() {
@Override
public void onStart() {
System.out.println("===");
System.out.println("开始下载了++++++++++++++");
} @Override
public void onLoading(long total, long current,
boolean isUploading) {
System.out.println(total + "=====");
System.out.println(current + "=====");
System.out.println(isUploading + "=====");
pa.setMax((int) total);
pa.setProgress((int) current);
} @Override
public void onFailure(HttpException error,
String msg) {
System.out.println(error + "+++++++" + msg
+ "+++++++++"); } @Override
public void onSuccess(
ResponseInfo<File> responseInfo) {
h.cancel();
installApk(path + "/badumap.apk"); } }); }
}); }
//安装的方法
private void installApk(String filename) {
File file = new File(filename);
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
String type = "application/vnd.android.package-archive";
intent.setDataAndType(Uri.fromFile(file), type);
startActivity(intent); }
//等到版本号的代码
private void getCode() { PackageManager manager; PackageInfo info = null; manager = this.getPackageManager(); try { info = manager.getPackageInfo(this.getPackageName(), 0);
String name = info.versionName;
int versionCode = info.versionCode;
String packageName = info.packageName;
Signature[] signatures = info.signatures;
System.out.println(name + "++++++++++" + versionCode + "+++++"
+ packageName + "========nnn" + signatures
+ "=============");
/*
* info.versionName;
*
* info.packageName;
*
* info.signatures;
*/ } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

  

下载app后自动安装程序的更多相关文章

  1. Android实例-IdHTTP下载(并实现自动安装)(XE10+小米2)

    相关资料: 1.群号 383675978 2.运行时提示"connection closed gracefully"错误原因与解决 http://www.delphifans.co ...

  2. Android开发必有功能,更新版本提示,检测是否有新版本更新。下载完成后进行安装。

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985,转载请说明出处. 给大家介绍个东西,MarkDown真的超级超级好用.哈哈.好了, 正题内容如下: 先 ...

  3. Android--用DownLoadManager下载完成后启动安装

    当我们用系统的服务DownLoadManager下载完成后,系统会发送一个广播,我们只需要注册一个广播,然后在广播里面写如一些相应的操作. 1.注册广播 completeReceiver = new ...

  4. 如何实现Outlook 2010 下载邮件后自动删除服务器上的邮件

    outlook2010---文件---信息---账户设置---选中要设置的帐号---双击点选要设置的邮箱---其他设置---高级---在服务器上保留邮件的副本---14天后删除服务器上的邮件副本,修改 ...

  5. c#中如何退出程序后自动重新启动程序

    //触发退出程序事件 private void button1_Click(object sender, EventArgs e)        {             Application.E ...

  6. 使用Inno Setup 打包.NET程序,并自动安装.Net Framework

    使用Inno Setup 打包.NET程序,并自动安装.Net Framework http://www.cnblogs.com/xiaogangqq123/archive/2012/03/19/24 ...

  7. Android(2)—Mono For Android App版本自动更新

    0.前言 公司Android查询的项目已经开始,整体采用Java后台+App前台来实现,由于项目小,App这块就我一个人开发,首先需要研究的是:Android版本的更新升级问题:本人经过近一周的学习整 ...

  8. 一个完整的Installshield安装程序实例-转

    一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品(一)---基本设置一 前言 Installshield可以说是最好的做安装程序的商业软件之一,不过因为功能的太过于强大,以至于 ...

  9. sencha touch 扩展篇之将sencha touch打包成安装程序(下)- 使用phonegap打包安装程序

        这讲我们来讲解下如何使用phonegapa创建项目环境并通过她们将sencha touch打包成app,这里我们只讲解打包android的apk,打包ios的过程有点类似,但是需要在mac环境 ...

随机推荐

  1. lunece全文检索的入门与简单优化

    2016-12-15 项目中需要加载下面几个工具包 1 package com.cn.shupu.util; import java.io.BufferedReader; import java.io ...

  2. 配置Office 365单点登录过程中的一些注意事项

    这些天一直在整O365单点登录的问题,其中涉及到了很多知识点,其中以ADFS,CA为主吧,IIS为辅.下面我就把这些天积累的一些经验写下来备用. 1. 申请证书不一定要通过“证书颁发机构Web注册”, ...

  3. Java学习记录-Jdk包简单介绍

    java.applet Java语言编写的一些小应用程序 java.awt AWT 是Abstract Window ToolKit (抽象窗口工具包)的缩写,这个工具包提供了一套与本地图形界面进行交 ...

  4. javascript 判断为true false

    如果逻辑对象无初始值或者其值为 0.-0.null."".false.undefined 或者 NaN,那么对象的值为 false.否则,其值为 true(即使当自变量为字符串 & ...

  5. ssh 文件权限影响登录

    进入/etc/ssh目录 需要保证文件访问控制权限如下: ssh_host_dsa_key 600 ssh_host_ras_key 600

  6. vs2010 2013 2015+ 必备插件精选(15个)

    转 http://www.spersky.com/post/vsPlugins.html 我目前主要用的是Hide Main Page——公司配给的电脑屏幕分辨率好小,还是1366*768的,去掉头可 ...

  7. Ubuntu 14.04 编译安装 boost 1.58

    简介 Boost is a set of libraries for the C++ programming language that provide support for tasks and s ...

  8. GIT远程仓库地址变更

    将VS2013的解决方案添加到GIT源代码管理后会增加.gitattributes和.gitignore 2个文件以及.git目录 设置远程地址的文件在.git目录下的config文件中 直接修改上图 ...

  9. JMeter学习-022-JMeter 分布式测试(性能测试大并发、远程启动解决方案)

    在使用 JMeter 进行性能测试时,难免遇到要求并发请求数比较的场景,此时单台测试机的配置(CPU.内存.带宽等)可能无法支持此性能测试场景.因而,此时 JMeter 提供的分布式测试功能就有了用武 ...

  10. Python学习【第六篇】运算符

    运算符 算数运算: a = 21 b = 10 c = 0 c = a + b print ("1 - c 的值为:", c) c = a - b print ("2 - ...