下载app后自动安装程序
其实很简单,只需要几行代码就好了,首先要到服务器下载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后自动安装程序的更多相关文章
- Android实例-IdHTTP下载(并实现自动安装)(XE10+小米2)
相关资料: 1.群号 383675978 2.运行时提示"connection closed gracefully"错误原因与解决 http://www.delphifans.co ...
- Android开发必有功能,更新版本提示,检测是否有新版本更新。下载完成后进行安装。
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985,转载请说明出处. 给大家介绍个东西,MarkDown真的超级超级好用.哈哈.好了, 正题内容如下: 先 ...
- Android--用DownLoadManager下载完成后启动安装
当我们用系统的服务DownLoadManager下载完成后,系统会发送一个广播,我们只需要注册一个广播,然后在广播里面写如一些相应的操作. 1.注册广播 completeReceiver = new ...
- 如何实现Outlook 2010 下载邮件后自动删除服务器上的邮件
outlook2010---文件---信息---账户设置---选中要设置的帐号---双击点选要设置的邮箱---其他设置---高级---在服务器上保留邮件的副本---14天后删除服务器上的邮件副本,修改 ...
- c#中如何退出程序后自动重新启动程序
//触发退出程序事件 private void button1_Click(object sender, EventArgs e) { Application.E ...
- 使用Inno Setup 打包.NET程序,并自动安装.Net Framework
使用Inno Setup 打包.NET程序,并自动安装.Net Framework http://www.cnblogs.com/xiaogangqq123/archive/2012/03/19/24 ...
- Android(2)—Mono For Android App版本自动更新
0.前言 公司Android查询的项目已经开始,整体采用Java后台+App前台来实现,由于项目小,App这块就我一个人开发,首先需要研究的是:Android版本的更新升级问题:本人经过近一周的学习整 ...
- 一个完整的Installshield安装程序实例-转
一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品(一)---基本设置一 前言 Installshield可以说是最好的做安装程序的商业软件之一,不过因为功能的太过于强大,以至于 ...
- sencha touch 扩展篇之将sencha touch打包成安装程序(下)- 使用phonegap打包安装程序
这讲我们来讲解下如何使用phonegapa创建项目环境并通过她们将sencha touch打包成app,这里我们只讲解打包android的apk,打包ios的过程有点类似,但是需要在mac环境 ...
随机推荐
- CSS实现元素水平/垂直居中的方法
首先,我们来了解水平居中,它有很多种方法,我们暂时先来了解其中的几种: 1. 在实现方案中,我们最熟悉的莫过于给元素定义一个宽度,然后使用margin: 1 2 3 4 body{ wi ...
- C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别
转自:http://www.cnblogs.com/leolis/p/3968943.html 在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为 整型(int)来讲, ...
- Oracle EBS Setup
1. Prevent close other forms after close original form
- IOS网络第二天 - 09-多值参数
*********** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @interface ...
- 规范和封装jdbc程序代码
JDBC 部分方法引用工具类 package it.cast.jdbc; import java.sql.Connection; import java.sql.DriverManager; impo ...
- AnjularJS异步编程 Promise和$q
Promise,是一种异步处理模式. js代码的函数嵌套会使得程序执行异步代码时很难调试.因为多重嵌套的函数无法确定何时触发回调. 如: funA(arg1,arg2,function(){ func ...
- spring mvc@RequestParam根据参数名获取传入参数值
在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取.这里主要 ...
- java容器collection的一些简单特点
1.List ArrayList 可随机访问元素,但中间插入和一处元素较慢 LinkedList 在中间进行的插入和删除操作代价较小,随机访问比ArrayList较慢 特性集比ArrayList大 2 ...
- python学习道路(day11note)(协程,同步与异步的性能区别,url爬网页,select,RabbitMq)
1.协程 #协程 又称微线程 是一种用户的轻量级线程 程序级别代码控制 就不用加机器 #不同函数 = 不同任务 A函数切到B函数没有进行cpu级别的切换,而是程序级别的切换就是协程 yelied #单 ...
- SMTP Error: Could not connect to SMTP host
PHPMailer是一个非常棒的开源邮件类,使用也非常简单,但是对于虚拟主机来说,往往要受到各种限制.刚才我在虚拟主机上使用PHPMailer就遇到一个“SMTP Error: Could not c ...