android 从服务器上获取APK并下载安装
简单的为新手做个分享。
网上有些资料,不过都是很零散,或是很乱的,有的人说看不懂。
一直有新手说 做到服务器更新APK时没有思路,这里做个简单的分享,希望有不同思路的可以讨论。
下面做个很简单的读取处理和讲解思路。
代码带有注释:
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(10 * 1000); //超时时间
connection.connect(); //连接
if (connection.getResponseCode() == 200) { //返回的响应码200,是成功.
File file = new File("/mnt/sdcard/yang/dujinyang.apk"); //这里我是手写了。建议大家用自带的类
file.createNewFile();
InputStream inputStream = connection.getInputStream();
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); //缓存
byte[] buffer = new byte[1024 * 10];
while (true) {
int len = inputStream.read(buffer);
publishProgress(len);
if (len == -1) {
break; //读取完
}
arrayOutputStream.write(buffer, 0, len); //写入
}
arrayOutputStream.close();
inputStream.close(); byte[] data = arrayOutputStream.toByteArray();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(data); //记得关闭输入流
fileOutputStream.close();
} } catch (MalformedURLException e) {
. e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
以上是读取APK文件并保存在了本地,InputStream转为FileOutputStream保存HttpURLConnection获取到的数据 。
那么只要再找到你的那个保存的路径就能实现安装了。
下面是安装和卸载的代码:
首先说下卸载:Uri packageURI = Uri.parse("package:com.demo.DUJINYANG");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);Environment拥有一些可以获取环境变量的方法
package:com.demo.DUJINYANG 这个形式是 package:程序完整的路径 (包名+程序名).
然后是 --安装: String str = "/Dujinyang.apk"; //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);
主要代码如下://打开APK程序代码 private void openFiles(File file) {
// TODO Auto-generated method stub
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);
}
当然拉 ,这里不仅一种方法:以下方法也是可行的--
//下载apk程序代码
protected File downLoadFile(String httpUrl) {
final String fileName = "dujinyang.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 fileOutput= new FileOutputStream(file);
byte[] buf = new byte[256];//分配byte
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 {
fileOutput.write(buf, 0, numRead);
} } else {
break;
} }
} conn.disconnect();//需要记得关闭连接
fileOutput.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) { e.printStackTrace();
} return file;
}
到这里 思路简单的理清 完了。
此时可以根据你自身的项目去整改。如果新手还有不懂的可以私聊。
--分享 希望大家有好的代码可以分享,共同讨论
android 从服务器上获取APK并下载安装的更多相关文章
- android 从服务器上获取APK下载安装
简单的为新手做个分享. 网上有些资料,不过都是很零散,或是很乱的,有的人说看不懂. 一直有新手说 做到服务器更新APK时没有思路,这里做个简单的分享,希望有不同思路的可以讨论. 下面做个很简单的读 ...
- SNF开发平台WinForm之十三-单独从服务器上获取PDF文件进行显示-SNF快速开发平台3.3-Spring.Net.Framework
1运行效果: 2开发实现: 如果需要单独显示PDF文件时用下面代码去实现,指定url地址. 地址: . 获取附件管理的实体对象: List<KeyValuePair<string, obj ...
- google play上获取apk文件
先说一种测试不通过的方法(chrome浏览器添加Direct APK downloader拓展程序),浪费了我很多的时间,结果发现根本用不了,记录一下过程给大家参考. 使用chrome浏览器,点击左上 ...
- Java从服务器上获取时间,动态在jsp页面显示
Java获取服务器时间,动态显示到jsp页面,大家都是到Java只能获取一次,到页面的时间是静态的,不过通过js和Java的合作,巧妙地实现此功能 本人是给电视做系统,客户要求页面能显示时间,因为电视 ...
- HTTP请求(GET与POST区别)和响应(get是从服务器上获取数据,post是向服务器传送数据,格式与举例都非常清楚)
HTTP有两部分组成:请求与响应,下面分别整理. 一.HTTP请求 1.HTTP请求格式: <request line> <headers> <blank line> ...
- 在阿里云服务器上(centos 8) 安装自己的MQTT服务器 (mosquitto)
layout: post title: 在阿里云服务器上(centos 8) 安装自己的MQTT服务器 (mosquitto) subtitle: date: 2020-3-2 author: Dap ...
- 【译】如何在 Android 5.0 上获取 SD卡 的读写权限
因为最近项目需要,涉及到 SD卡 的读写操作,然而申请 <!-- 读写权限 --> <uses-permission android:name="android.permi ...
- android 向服务器上传
采用数据流的格式向服务器上传. 代码如下: private void upload(String requestURL) { //参数requestU ...
- 93服务器上获取json数据
jdf u -p上传html文件,上传到page域名下:jdf u 上传css和js 上传到misc域名下: json数据放在html下,因为ajax请求是按照html路径走的,所以json数据放在h ...
随机推荐
- android 支持分组和联系人展示的一个小样例
先看效果图: 要实现这个效果,activity必须实现ExpandableListActivity @Override public void onCreate(Bundle savedInstanc ...
- 深入了解Nginx之Nginx与Python(1)
6 Python和Nginx 6.1 简介FastCGI FastCGI(Fast Common Gateway Interface)是基于CGI上的改进,是CGI的一种演变产物.虽然目的是保持同样的 ...
- 基于RYU控制器(controller)上的simple-switch 的APP做的測试-SDN/OpenFlow
近期一直在学习RYU控制器,在使用的过程中,发现有下面几方面的长处:RYU控制器全然使用Python语言编写,在理解起来和上手速度上是挺快的:RYU控制器的总体架构清晰明了,在日后有时间我会整理一个关 ...
- C# inherit
Case:class A has a construct. class B is inherit from class A and B also has a construct. What's the ...
- MySQl 存储过程+游标
DROP PROCEDURE IF exists pro_Insertflightplan_stat; create procedure pro_Insertflightplan_stat(exec ...
- 初学MVC
学习MVC基础:C#. ADO.NET .html.javascript.ASP.Net .WebFrom MVC模式两种理解:一种是表现模式,另一种是架构模式.它将应用程序分成三个主要的组件:视图( ...
- C# Best Practices - Creating Good Properties
Coding Properties Code in the Getter Check the user's credentials Check application state Format the ...
- 使用autoCompleteTextView以及MultiAutoCompleteTextView实现自动匹配输入内容
一:autoCompleteTextView 1:控件属性设置: 注意添加:android:completionThreshold="1"来设置输入几个字符的时候开始显示匹配的内容 ...
- SQLserver查询数据类型为ntext是空或NULL值的方法
--为空的值text ntext select * from lf_newsNg_utf where datalength(newsContentE)=0 or datalength(newsCont ...
- HAMA
http://hama.apache.org/run_examples.html http://www.binospace.com/ http://57832638.iteye.com/blog/20 ...