其实很简单,只需要几行代码就好了,首先要到服务器下载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. ZK 最少限度加载页面js文件

    官方文档说明: ZK Developer's Reference文档,章节为Minimize Number of JavaScript Files to Load,按照文档步骤执行,最后需在 web. ...

  2. include、require、include_once和require_once理解

    都是在当前文件中包含引入并运行指定文件,include和require的不通之处仅仅在于发生错误时include产生一个警告脚本继续执行,而require产生一个致命的错误,脚本停止运行.有了once ...

  3. 重新梳理HTML基础知识

    缘起 HTML(HyperText Markup Language超文本标记语言)是用于构建web页面的标记语言和通用标准.它并不是一项新的发明,因为超文本(具有超链接的文本)和标记语言(用于电子文档 ...

  4. 掌握Thinkphp3.2.0----模版基础

    我们将要学习一下 ThinkPHP 模版,ThinkPHP 内置了一个基于 XML 的性能卓越的模版引擎 ThinkTemplate,使用了动态编译和缓存技术,支持自定义标签库, 其实我还体会不到其中 ...

  5. Sharepoint client model 中出现Cannot invoke HTTP DAV request. There is a pending query 的解决办法

    由于近期在某项目中使用sharepoint client 对象模型做项目 在sharepoint 2010环境下正常,但迁移到sharepoint 2013后报错,提示如下 Cannot invoke ...

  6. angular路由 模块 依赖注入

    1.模块 var helloModule=angular.module('helloAngular',[]); helloModule.controller('helloNgCrtl',['$scop ...

  7. 《Linux内核分析》第八周 进程的切换和系统的一般执行过程

    [刘蔚然 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK EIGHT ...

  8. 【leetcode】两数之和

    https://leetcode.com/problems/two-sum/ Example: Given nums = [2, 7, 11, 15], target = 9, Because num ...

  9. css控制文字大小及颜色、字体

    字体:font-style:italic:斜体         font-weight:bold:加粗         font-size:30px:大小         line-height:30 ...

  10. nginx 配置优化(简单)

    配置文件     正常运行的必备配置:         1.user username [groupname]:(推荐nginx)         以那个用户身份运行,以在configure指定的用户 ...