ImageVerifierCode 换一换
格式:DOCX , 页数:12 ,大小:25.53KB ,
资源ID:21813842      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/21813842.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Android程序的版本检测与更新Word文档格式.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Android程序的版本检测与更新Word文档格式.docx

1、否则新的无法替换旧的,提示安装未完成。- 天气太冷,咯咯咯 -这个功能做在菜单上,触发代码如下:/=/ 菜单/= private static final String urlApk = http:/3g.*.com/tool/*.apk;private static final String urlVer = /3g.*.com/tool/ver_apk.xmlOverridepublic boolean onCreateOptionsMenu(Menu menu) menu.add(Menu.NONE, Menu.FIRST + 1, 5, 检测更新).setIcon( android.R

2、.drawable.ic_menu_upload); menu.add(Menu.NONE,Menu.FIRST+2,4,退出).setIcon(android.R.drawable.ic_lock_power_off); return true;public boolean onOptionsItemSelected(MenuItem item) switch (item.getItemId() case Menu.FIRST + 1: Toast.makeText(this, 正在检测版本, Toast.LENGTH_LONG).show(); UpdateVer uv = new Upd

3、ateVer(urlApk,urlVer,MainActivity.this); uv.checkVer(); break; case Menu.FIRST + 2: confirmExit(); return false;检测更新因为代码比较多,写成一个类进行封装UpdateVer.javapackage android.*;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.ProgressDialog;import android.c

4、ontent.Context;import android.content.DialogInterface;import android.content.Intent;import .Uri;import android.os.AsyncTask;import android.util.Log;import android.webkit.URLUtil;import android.widget.Toast;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;impor

5、t java.io.IOException;import java.io.InputStream;import .MalformedURLException;import .URL;import .URLConnection;import java.util.Properties;import org.xml.sax.InputSource;import java.text.SimpleDateFormat;import java.util.Date;public class UpdateVer extends Activity private static final String TAG

6、= DOWNLOADAPK private String PastVersion; private String NowVersion; public ProgressDialog pBar; private String currentFilePath = private String fileEx= private String fileNa= private String strURL= private String VersionUri = private Context mContext; private final String fileVer = ver.cfg public U

7、pdateVer(String urlapk,String urlver,final Context context) SimpleDateFormat df = new SimpleDateFormat(yyyyMMddHHmmss); String ver = ver= + df.format(new Date();/主要是避开手机的缓存 strURL = urlapk + ver; VersionUri = urlver + ver; mContext = context; public void checkVer() / 解析Version网页,获取版本号 getVersionxml(

8、VersionUri); private void compareVer() load(); /当有最新版本的时候 if(PastVersion != null & !PastVersion.equals(NowVersion) Dialog dialog = new AlertDialog.Builder(mContext).setTitle(系统更新) .setMessage(String.format(发现新版本%s,目前版本为%s,请更新!,NowVersion,PastVersion)/ 设置内容 / 设置确定按钮 .setPositiveButton(确定 ,new DialogI

9、nterface.OnClickListener() Override public void onClick(DialogInterface dialog, int which) pBar = new ProgressDialog(mContext); pBar.setTitle(正在下载 pBar.setMessage(请稍候. pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER); fileEx = strURL.substring(strURL.lastIndexOf(.) + 1,strURL.length().toLowerCase

10、(); fileEx = fileEx.substring(0,fileEx.lastIndexOf(); fileNa = strURL.substring(strURL.lastIndexOf(/) + 1,strURL.lastIndexOf( getFile(strURL); ).setNegativeButton(取消, new DialogInterface.OnClickListener() int whichButton) / 点击按钮之后退出程序 ).create();/ 创建 / 显示对话框 dialog.show(); else Toast.makeText(mConte

11、xt, String.format(当前为最新版本%s,PastVersion), Toast.LENGTH_LONG).show(); private void getFile(final String strPath) pBar.show(); try if (strPath.equals(currentFilePath) ) getDataSource(strPath); currentFilePath = strPath; Runnable r = new Runnable() public void run() catch (Exception e) Log.e(TAG, e.get

12、Message(), e); ; new Thread(r).start(); catch(Exception e) e.printStackTrace(); /*取得远程文件*/ private void getDataSource(String strPath) throws Exception if (!URLUtil.isNetworkUrl(strPath) Log.d(Tag,error else /*取得URL*/ URL myURL = new URL(strPath); /*建立联机*/ URLConnection conn = myURL.openConnection();

13、 conn.connect(); /*InputStream 下载文件*/ InputStream is = conn.getInputStream(); if (is = null) tag throw new RuntimeException(没有读取到文件内容 /*建立临时文件*/ File myTempFile = File.createTempFile(fileNa, + fileEx); myTempFile.getAbsolutePath(); /*将文件写入临时盘*/ FileOutputStream fos = new FileOutputStream(myTempFile)

14、; byte buf = new byte128; do int numread = is.read(buf); if (numread = 0) fos.write(buf, 0, numread); while (true); /*打开文件进行安装*/ openFile(myTempFile); try is.close(); catch (Exception ex) Log.e(TAG, error: + ex.getMessage(), ex); /* 在手机上打开文件 */ private void openFile(File f) pBar.cancel(); Intent int

15、ent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); /* 调用getMIMEType()来取得MimeType */ String type = getMIMEType(f); /* 设定intent的file与MimeType */ intent.setDataAndType(Uri.fromFile(f),type); mContext.startActivity(intent); /* 判断文件Mi

16、meType的method */ private String getMIMEType(File f) String type = String fName = f.getName(); /* 取得扩展名 */ String end = fName.substring(fName.lastIndexOf()+1,fName.length().toLowerCase(); /* 按扩展名的类型决定MimeType */ if(end.equals(m4a) | end.equals(mp3midxmfoggwav) type = audio else if(end.equals(3gp) | e

17、nd.equals(mp4videojpggifpngjpegbmpimageapk) /* android.permission.INSTALL_PACKAGES */ application/vnd.android.package-archive* /*如果无法直接打开,就跳出软件清单给使用者选择 */ if(!end.equals( type += /* return type; private void getVersionxml(String resourceUrl) GetVer gv = new GetVer(); gv.execute(resourceUrl); private

18、 boolean load() Properties properties = new Properties(); try InputStream stream = mContext.getAssets().open(fileVer); /FileInputStream stream = mContext.openFileInput(fileVer); /读取文件内容 properties.load(stream); catch (FileNotFoundException e) catch(IOException e) catch(Exception e) PastVersion = Str

19、ing.valueOf(properties.get(Version).toString(); /= / GetVer /= class GetVer extends AsyncTask Override protected String doInBackground(String. urlVer) String db = null; URL url = null; try url = new URL(urlVer0); catch (MalformedURLException e) InputSource is = null; is = new InputSource(url.openStr

20、eam(); is.setEncoding(UTF-8 db = SAXGetVersionService.readRssXml(is); catch (Exception e) return db; protected void onCancelled() super.onCancelled(); protected void onPostExecute(String result) NowVersion = result; compareVer();AndroidManifest.xml要加上几句 android.permission.INSTALL_PACKAGES/android.permission.MOUNT_UNMOUNT_FILESYSTEMSandroid.permission.WRITE_EXTERNAL_STORAGEandroid.permission.RESTART_PACKAGESandroid.permission.READ_PHONE_STATEandroid.permission.ACCESS_NETWORK_STATEandroid.permission.ACCESS_COARSE_LOCATIONandroid.permission.ACCESS_WIFI_STATE参考文章:

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1