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

Android App 更新

2016-11-02 14:24 239 查看
作为安卓的一名新人,对于app的更新从一开始是惧怕的,因为没有接触过,感觉会是一个很有难度的技术,其实当了解真正的原理后才发现,其实真得很简单.一下就简单的描述一下app的更新原理.
* 在这里只阐述客户端的内容,服务端的内容就无需了解太细致了.
* app更新的本质就是,把一个.apk的文件打开就可以了.就是这么简单.那么我们要怎么具体操作呢.
* 1.get/post请求和服务端交互,服务端会返回给你一个状态码,你们协商,如果为0就是不更新,如果为1就是需要更新,如果为2就是强制更新,那么当返回的状态码为1或者2时,会对应的给你一个url地址,这个地址也就是apk的下载地址了.
* 2.我们有了apk的下载地址就很简单了,get请求,文件流下载保存就不用多说了.
* 3.intent.setDataAndType("保存的文件名称","文件类型"),用intent打开就算是安装了,然后app就更新啦,是不是很简单了.
*
* 上面说了三点,有些基础的第一第二点都可以直接忽略掉了,那么就是一个打开.apk文件的过程.
* 解释一下"application/vnd.android.package-archive"这个,是.apk的所对应的MIME类型
*
* 下面会给一个数组写的很清楚,那么内容就这么简单,大家可以尝试一下.备注(我自己的搭的WampServer,一键配置的服务端,把要更新的.apk文件放在www目录下就可以了).


//{后缀名,MIME类型}
{".3gp",    "video/3gpp"},
{".apk",    "application/vnd.android.package-archive"},
{".asf",    "video/x-ms-asf"},
{".avi",    "video/x-msvideo"},
{".bin",    "application/octet-stream"},
{".bmp",    "image/bmp"},
{".c",  "text/plain"},
{".class",  "application/octet-stream"},
{".conf",   "text/plain"},
{".cpp",    "text/plain"},
{".doc",    "application/msword"},
{".docx",   "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{".xls",    "application/vnd.ms-excel"},
{".xlsx",   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".exe",    "application/octet-stream"},
{".gif",    "image/gif"},
{".gtar",   "application/x-gtar"},
{".gz", "application/x-gzip"},
{".h",  "text/plain"},
{".htm",    "text/html"},
{".html",   "text/html"},
{".jar",    "application/java-archive"},
{".java",   "text/plain"},
{".jpeg",   "image/jpeg"},
{".jpg",    "image/jpeg"},
{".js", "application/x-javascript"},
{".log",    "text/plain"},
{".m3u",    "audio/x-mpegurl"},
{".m4a",    "audio/mp4a-latm"},
{".m4b",    "audio/mp4a-latm"},
{".m4p",    "audio/mp4a-latm"},
{".m4u",    "video/vnd.mpegurl"},
{".m4v",    "video/x-m4v"},
{".mov",    "video/quicktime"},
{".mp2",    "audio/x-mpeg"},
{".mp3",    "audio/x-mpeg"},
{".mp4",    "video/mp4"},
{".mpc",    "application/vnd.mpohun.certificate"},
{".mpe",    "video/mpeg"},
{".mpeg",   "video/mpeg"},
{".mpg",    "video/mpeg"},
{".mpg4",   "video/mp4"},
{".mpga",   "audio/mpeg"},
{".msg",    "application/vnd.ms-outlook"},
{".ogg",    "audio/ogg"},
{".pdf",    "application/pdf"},
{".png",    "image/png"},
{".pps",    "application/vnd.ms-powerpoint"},
{".ppt",    "application/vnd.ms-powerpoint"},
{".pptx",   "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{".prop",   "text/plain"},
{".rc", "text/plain"},
{".rmvb",   "audio/x-pn-realaudio"},
{".rtf",    "application/rtf"},
{".sh", "text/plain"},
{".tar",    "application/x-tar"},
{".tgz",    "application/x-compressed"},
{".txt",    "text/plain"},
{".wav",    "audio/x-wav"},
{".wma",    "audio/x-ms-wma"},
{".wmv",    "audio/x-ms-wmv"},
{".wps",    "application/vnd.ms-works"},
{".xml",    "text/plain"},
{".z",  "application/x-compress"},
{".zip",    "application/x-zip-compressed"},
{"",        "*/*"}


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

downFile("http://192.168.16.100:80/apk/File_post.apk");//我自己配的WampServer的www目录下的.apk文件的本地地址.安卓端的也可以自己尝试一下配置一个服务端,很容易.

}

//下载文件
void downFile(final String url) {
new Thread(){
public void run() {

HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response;

try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if(is != null){
File file = new File(Environment.getExternalStorageDirectory(),//获取sd卡上的目录
"File_post1.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
}
}
fileOutputStream.flush();
if(fileOutputStream != null){
fileOutputStream.close();
}

//安装文件
update();

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

};
}.start();

}

//安装文件
protected void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"File_post1.apk")),"application/vnd.android.package-archive");//打开一个文件,application/vnd.android.package-archive是MIME类型文件类型
startActivity(intent);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息