您的位置:首页 > 移动开发 > Android开发

android 从服务器上获取APK下载安装

2013-06-18 16:37 435 查看


简单的为新手做个分享。

网上有些资料,不过都是很零散,或是很乱的,有的人说看不懂。一直有新手说做到服务器更新APK时没有思路,这里做个简单的分享,希望有不同思路的可以讨论。

下面做个很简单的读取处理和讲解思路。代码带有注释:

try{ URLurl=newURL(params[0]); HttpURLConnectionconnection=(HttpURLConnection)url .openConnection(); connection.setConnectTimeout(10*1000);//超时时间 connection.connect();//连接 if(connection.getResponseCode()==200){//返回的响应码200,是成功. Filefile=newFile("/mnt/sdcard/yang/dujinyang.apk");//这里我是手写了。建议大家用自带的类 file.createNewFile(); InputStreaminputStream=connection.getInputStream(); ByteArrayOutputStreamarrayOutputStream=newByteArrayOutputStream();//缓存 byte[]buffer=newbyte[1024*10]; while(true){ intlen=inputStream.read(buffer); publishProgress(len); if(len==-1){ break;//读取完 } arrayOutputStream.write(buffer,0,len);//写入 } arrayOutputStream.close(); inputStream.close(); byte[]data=arrayOutputStream.toByteArray(); FileOutputStreamfileOutputStream=newFileOutputStream(file); fileOutputStream.write(data);//记得关闭输入流 fileOutputStream.close(); } }catch(MalformedURLExceptione){ .e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }

以上是读取APK文件并保存在了本地,InputStream转为FileOutputStream保存HttpURLConnection获取到的数据。

那么只要再找到你的那个保存的路径就能实现安装了。

下面是安装和卸载的代码:首先说下卸载:

UripackageURI=Uri.parse("package:com.demo.DUJINYANG");

IntentuninstallIntent=newIntent(Intent.ACTION_DELETE,packageURI);

startActivity(uninstallIntent);

Environment拥有一些可以获取环境变量的方法package:com.demo.DUJINYANG这个形式是package:程序完整的路径(包名+程序名).

然后是--安装:

Stringstr="/Dujinyang.apk";//APK的名字

StringfileName=Environment.getExternalStorageDirectory()+str;//我们上面说到路径

Intentintent=newIntent(Intent.ACTION_VIEW);

intent.setDataAndType(Uri.fromFile(newFile(fileName)),

"application/vnd.android.package-archive");

startActivity(intent);

主要代码如下://打开APK程序代码

privatevoidopenFiles(Filefile){
//TODOAuto-generatedmethodstub
Intentintent=newIntent();
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程序代码
protectedFiledownLoadFile(StringhttpUrl){
finalStringfileName="dujinyang.apk";
FiletmpFile=newFile("/sdcard/update");
if(!tmpFile.exists()){
tmpFile.mkdir();//创建文件夹
}
finalFilefile=newFile("/sdcard/update/"+fileName);

try{
URLurl=newURL(httpUrl);
try{
HttpURLConnectionconn=(HttpURLConnection)url
.openConnection();
InputStreamis=conn.getInputStream();
FileOutputStreamfileOutput=newFileOutputStream(file);
byte[]buf=newbyte[256];//分配byte
conn.connect();
doublecount=0;
if(conn.getResponseCode()>=400){
Toast.makeText(Main.this,"连接超时",Toast.LENGTH_SHORT)
.show();
}else{
while(count<=100){
if(is!=null){
intnumRead=is.read(buf);
if(numRead<=0){
break;
}else{
fileOutput.write(buf,0,numRead);
}

}else{
break;
}

}
}

conn.disconnect();//需要记得关闭连接
fileOutput.close();
is.close();
}catch(IOExceptione){
e.printStackTrace();
}
}catch(MalformedURLExceptione){ e.printStackTrace();
}

returnfile;
}


到这里思路简单的理清完了。

此时可以根据你自身的项目去整改。如果新手还有不懂的可以私聊。

--分享希望大家有好的代码可以分享,共同讨论
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: