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); } }
相关文章推荐
- 编写Android app更新模块遇到的问题分析与总结
- Android学习系列(2)--App自动更新之通知栏下载
- Android学习系列(2)--App自动更新之通知栏下载
- 【Android】Web开发之通知栏下载更新APP
- android应用程序安全卫士——2、通过服务器xml文件,判断app是否需要更新
- android 触发AppWidget上控件事件来更新AppWidget
- Android App自动更新服务
- android launcher桌面数字时钟及appwidget更新问题
- Android APP 版本更新通知代码
- 【Android】App自动更新之通知栏下载
- Android学习系列(3)--App自动更新之自定义进度视图和内部存储
- [Android笔记] 关于 AppWidget 动态更新:RemoteViews 更新缓慢&内存溢出
- Android学习系列(2)--App自动更新之通知栏下载
- Android如何更新app的版本(中级)
- Android-接受来自Appwidget的广播、更新Appwidget控件的状态
- 编写Android app更新模块遇到的问题分析与总结
- Android app“版本更新”功能的前后端实现
- Android-接受来自Appwidget的广播、更新Appwidget控件的状态
- Android项目实战--手机卫士03--完成app更新的逻辑和主界面
- Android 自动更新APP时确定安装后没有显示安装完成页面