Android 网络应用重点———使用HttpGet 下载apk文件并安装
2011-08-26 17:17
1396 查看
本例使用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){}
}
}
示意图
1.
2.
3.
4.
具体代码请参见 ch09_remoteinstallapk工程,在这里要先运行服务器端oa工程,可以输入http://192.168.1.123/oa/apk/action.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){}
}
}
示意图
1.
2.
3.
4.
具体代码请参见 ch09_remoteinstallapk工程,在这里要先运行服务器端oa工程,可以输入http://192.168.1.123/oa/apk/action.apk看看效果
相关文章推荐
- Android 网络应用重点———使用HttpGet 下载apk文件并安装
- Android 网络应用重点———使用HttpGet 下载apk文件并安装
- Android 网络应用重点———使用HttpURLConnection 上传文件
- Android 网络应用重点———使用HttpURLConnection 上传文件
- android 应用升级 下载安装包 完成后 自动安装apk文件
- android 应用升级 下载安装包 完成后 自动安装apk文件
- 如何从Google Play下载Android应用的APK安装文件?
- 使用KVM安装windows大文件下载出现网络使用0%的情况说明
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- 安装APK文件到Android模拟器和Android sdcard的使用
- 安装APK文件到Android模拟器和Android sdcard的使用
- Android - HttpURLConnection下载网络文件,加载网络图片
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
- 12_Android中HttpClient的应用,doGet,doPost,doHttpClientGet,doHttpClient请求,另外借助第三方框架实现网络连接的应用,
- 安卓中使用HTTP从网络下载文件到SD卡程序
- OpenShift Redhat免费空间SSH登录管理和使用:下载文件安装程序和应用
- android使用http下载文件
- android+https网址屏蔽证书+IntentService+NotifyManager通知栏+下载apk文件+在通知栏显示下载进度+完成之后提示安装
- Java基础知识强化之网络编程笔记18:Android网络通信之 使用HttpClient的Post / Get 方式读取网络数据(基于HTTP通信技术)
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)