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