其实很简单,只需要几行代码就好了,首先要到服务器下载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. Junit测试 - Spring的配置

    第一种: @ContextConfiguration(locations = {"classpath*:/spring-core.xml"}) public class UserM ...

  2. css 设置圆角

    CSS3 圆角(border-radius) -moz(例如 -moz-border-radius)用于Firefox -webkit(例如:-webkit-border-radius)用于Safar ...

  3. 跳转到下一个activity

    /* * 第一个跳转代码 */ button.setOnClickListener(new OnClickListener() { @Override public void onClick(View ...

  4. alternatives命令用法

    alternatives是Linux下的一个功能强大的命令.只能在根权限下执行.如系统中有几个命令功能十分类似,却又不能随意删除,那么可以用替代指定一个全局的设置.alternatives常用于同一个 ...

  5. 使用github的使用,利用git shell命令行模式进行操作

    一.登录到git,新建一个版本仓库 二.在"Repository name"一栏里填写版本仓库的名称,如"test",Description栏是描述,可填可不填 ...

  6. c#实现万年历示例分享 万年历农历查询

    cs.cs(类页面) using System;using System.Collections.Generic;using System.Linq;using System.Web; namespa ...

  7. 【爬虫】BeautifulSoup之爬取百度贴吧的帖子

    在网上看到爬百度贴吧的例子,仿照写了一个用BeautifulSoup实现的,直接上代码吧 #coding:gbk import urllib2 from bs4 import BeautifulSou ...

  8. output_buffering开启

    ecshop后台出现Warning: Cannot modify header information - headers already sent by 修改完PHP.ini配置文件后,一定要重启服 ...

  9. css3知识

    一.box-sizing 属性 规定两个并排的带边框的框 二.align-items (适用于父类容器上) 设置或检索弹性盒子元素在侧轴(纵轴)方向上的对齐方式. 值: flex-start:弹性盒子 ...

  10. 深入理解js的prototype以及prototype的一些应用

    上一篇讲了js的prototype概念,在这里回顾一下prototype的定义: prototype是函数的一个属性,并且是函数的原型对象.引用它的必然是函数,这个应该记住. 但是,很奇怪,各位看官, ...