Android 网络应用重点———使用HttpGet 下载apk文件并安装
来源:互联网 发布:centos 7 snmp配置 编辑:程序博客网 时间:2024/05/22 07:01
本例使用HttpGet 从服务器端下载一个apk文件,然后自动将apk安装到手机上
下载文件原理: 先获得一个InputStream,读取到数据,再写入到目的地(通常写到SD卡), 概括起来也就是先读再写
主要代码如下:
public class Main extends Activity implements OnClickListener
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnDownloadInstallApk = (Button) findViewById(R.id.btnDownloadInstallApk);
btnDownloadInstallApk.setOnClickListener(this);
}
//安装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); //浏览网页的Action(动作)
String type = "application/vnd.android.package-archive";
intent.setDataAndType(Uri.fromFile(file), type); //设置数据类型
startActivity(intent);
}
@Override
public void onClick(View view)
{
//下载文件 存放目的地
String downloadPath = Environment.getExternalStorageDirectory().getPath() + "/download_cache";
String url = "http://192.168.1.123/oa/apk/action.apk";
File file = new File(downloadPath);
if(!file.exists())
file.mkdir();
HttpGet httpGet = new HttpGet(url);
try
{
HttpResponse httpResponse = new DefaultHttpClient().execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
InputStream is = httpResponse.getEntity().getContent();
// 开始下载apk文件
FileOutputStream fos = new FileOutputStream(downloadPath + "/action.apk");
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read(buffer)) != -1)
{
fos.write(buffer, 0, count);
}
fos.close();
is.close();
//安装 apk 文件
installApk(downloadPath+ "/action.apk");
}
} catch (Exception e){}
}
}
下载文件原理: 先获得一个InputStream,读取到数据,再写入到目的地(通常写到SD卡), 概括起来也就是先读再写
主要代码如下:
public class Main extends Activity implements OnClickListener
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnDownloadInstallApk = (Button) findViewById(R.id.btnDownloadInstallApk);
btnDownloadInstallApk.setOnClickListener(this);
}
//安装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); //浏览网页的Action(动作)
String type = "application/vnd.android.package-archive";
intent.setDataAndType(Uri.fromFile(file), type); //设置数据类型
startActivity(intent);
}
@Override
public void onClick(View view)
{
//下载文件 存放目的地
String downloadPath = Environment.getExternalStorageDirectory().getPath() + "/download_cache";
String url = "http://192.168.1.123/oa/apk/action.apk";
File file = new File(downloadPath);
if(!file.exists())
file.mkdir();
HttpGet httpGet = new HttpGet(url);
try
{
HttpResponse httpResponse = new DefaultHttpClient().execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
InputStream is = httpResponse.getEntity().getContent();
// 开始下载apk文件
FileOutputStream fos = new FileOutputStream(downloadPath + "/action.apk");
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read(buffer)) != -1)
{
fos.write(buffer, 0, count);
}
fos.close();
is.close();
//安装 apk 文件
installApk(downloadPath+ "/action.apk");
}
} catch (Exception e){}
}
}
- Android 网络应用重点———使用HttpGet 下载apk文件并安装
- Android 网络应用重点———使用HttpGet 下载apk文件并安装
- Android 网络应用重点———使用HttpURLConnection 上传文件
- Android 网络应用重点———使用HttpURLConnection 上传文件
- Android下载apk文件并安装
- DownloadManager——Android 下载 apk 包并安装
- 下载并安装apk文件
- Android——下载apk文件,并在通知栏显示下载进度
- Android实现应用下载并自动安装apk包
- Android实现应用下载并自动安装apk包
- Android实现应用下载并自动安装apk包
- Android实现应用下载并自动安装apk包
- Android实现应用下载并自动安装apk包
- Android实现应用下载并自动安装apk包
- Android实现应用下载并自动安装apk包
- Android实现应用下载并自动安装apk包
- android下载apk并安装
- Android下载并安装APK
- JasperReport学习笔记7-导出成其它格式(pdf,xls,html)
- 随机变量:从另一个方面看问题,或许就不一样了
- 使用逻辑数据库PCH的时候,如何实现一次只能选择一个OBJID的效果?
- 对Android4.0中Launcher2一些调试记录
- bat命令复制文件夹实现备份的小例子
- Android 网络应用重点———使用HttpGet 下载apk文件并安装
- sscanf,sscanf_s及其相关用法
- 一个绿色版本的wmnp包(windows下快速构建php开发环境)
- ArcGIS Runtime数据加载及编辑同步
- PrintWriter的缓冲效果
- JasperReport学习笔记8-JasperReport和Hibernate,Struts的整合
- Java new project 注意事项
- C++友元
- ie中的window模式窗口