Android 提示版本更新的实现

来源:互联网 发布:js如何隐藏鼠标指针 编辑:程序博客网 时间:2024/06/06 02:09

步骤:

1.检测当前版本的信息AndroidManifest.xml-->manifest-->android:versionName。

2.从服务器获取版本号(版本号存在于xml文件中)并与当前检测到的版本进行匹配,如果不匹配,提示用户进行升级,如果匹配则进入程序主界面。

3.当提示用户进行版本升级时,如果用户点击了确定,系统将自动从服务器上下载并进行自动升级,如果点击取消将进入程序主界面。

Android提示版本更新的实现

Android提示版本更新的实现

Android提示版本更新的实现

Android提示版本更新的实现

01获取当前程序的版本号:
02 
031./* 
042. * 获取当前程序的版本号  
053. */  
064.private String getVersionName() throwsException{  
075.    //获取packagemanager的实例    
086.    PackageManager packageManager = getPackageManager();  
097.    //getPackageName()是你当前类的包名,0代表是获取版本信息   
108.    PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);  
119.    returnpackInfo.versionName;   
1210.}
01获取服务器端的版本号:
021./* 
032. * 用pull解析器解析服务器返回的xml文件 (xml封装了版本号) 
043. */  
054.public static UpdataInfo getUpdataInfo(InputStream is) throwsException{  
065.    XmlPullParser  parser = Xml.newPullParser();    
076.    parser.setInput(is, "utf-8");//设置解析的数据源    
087.    inttype = parser.getEventType();  
098.    UpdataInfo info = newUpdataInfo();//实体   
109.    while(type != XmlPullParser.END_DOCUMENT ){  
1110.        switch(type) {  
1211.        caseXmlPullParser.START_TAG:  
1312.            if("version".equals(parser.getName())){  
1413.                info.setVersion(parser.nextText()); //获取版本号   
1514.            }else if("url".equals(parser.getName())){  
1615.                info.setUrl(parser.nextText()); //获取要升级的APK文件   
1716.            }else if("description".equals(parser.getName())){  
1817.                info.setDescription(parser.nextText()); //获取该文件的信息   
1918.            }  
2019.            break;  
2120.        }  
2221.        type = parser.next();  
2322.    }  
2423.    returninfo;  
2524.}
01从服务器下载apk:
021.public static File getFileFromServer(String path, ProgressDialog pd) throwsException{  
032.    //如果相等的话表示当前的sdcard挂载在手机上并且是可用的   
043.    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){  
054.        URL url = newURL(path);  
065.        HttpURLConnection conn =  (HttpURLConnection) url.openConnection();  
076.        conn.setConnectTimeout(5000);  
087.        //获取到文件的大小    
098.        pd.setMax(conn.getContentLength());  
109.        InputStream is = conn.getInputStream();  
1110.        File file = newFile(Environment.getExternalStorageDirectory(), "updata.apk");  
1211.        FileOutputStream fos = newFileOutputStream(file);  
1312.        BufferedInputStream bis = newBufferedInputStream(is);  
1413.        byte[] buffer = newbyte[1024];  
1514.        intlen ;  
1615.        inttotal=0;  
1716.        while((len =bis.read(buffer))!=-1){  
1817.            fos.write(buffer, 0, len);  
1918.            total+= len;  
2019.            //获取当前下载量   
2120.            pd.setProgress(total);  
2221.        }  
2322.        fos.close();  
2423.        bis.close();  
2524.        is.close();  
2625.        returnfile;  
2726.    }  
2827.    else{  
2928.        returnnull;  
3029.    }  
3130.}

匹配、下载、自动安装:

01/*
02 * 从服务器获取xml解析并进行比对版本号 
03 */ 
04public class CheckVersionTask implementsRunnable{ 
05   
06    public voidrun() { 
07        try
08            //从资源文件获取服务器 地址   
09            String path = getResources().getString(R.string.serverurl); 
10            //包装成url的对象   
11            URL url = newURL(path); 
12            HttpURLConnection conn =  (HttpURLConnection) url.openConnection();  
13            conn.setConnectTimeout(5000); 
14            InputStream is =conn.getInputStream();  
15            info =  UpdataInfoParser.getUpdataInfo(is); 
16               
17            if(info.getVersion().equals(versionname)){ 
18                Log.i(TAG,"版本号相同无需升级"); 
19                LoginMain(); 
20            }else
21                Log.i(TAG,"版本号不同 ,提示用户升级 "); 
22                Message msg = newMessage(); 
23                msg.what = UPDATA_CLIENT; 
24                handler.sendMessage(msg); 
25            
26        catch(Exception e) { 
27            // 待处理   
28            Message msg = newMessage(); 
29            msg.what = GET_UNDATAINFO_ERROR; 
30            handler.sendMessage(msg); 
31            e.printStackTrace(); 
32        }  
33    
34}  
01Handler handler = newHandler(){     
02    @Override 
03    public voidhandleMessage(Message msg) { 
04        // TODO Auto-generated method stub  
05        super.handleMessage(msg); 
06        switch(msg.what) { 
07        caseUPDATA_CLIENT: 
08            //对话框通知用户升级程序   
09            showUpdataDialog(); 
10            break
11            caseGET_UNDATAINFO_ERROR: 
12                //服务器超时   
13                Toast.makeText(getApplicationContext(), "获取服务器更新信息失败"1).show(); 
14                LoginMain(); 
15            break;   
16            caseDOWN_ERROR: 
17                //下载apk失败  
18                Toast.makeText(getApplicationContext(), "下载新版本失败"1).show(); 
19                LoginMain(); 
20            break;   
21            
22    
23};
01/*
02 
03 * 弹出对话框通知用户更新程序 
04 
05 * 弹出对话框的步骤:
06 *  1.创建alertDialog的builder.  
07 *  2.要给builder设置属性, 对话框的内容,样式,按钮
08 *  3.通过builder 创建一个对话框
09 *  4.对话框show()出来  
10 */ 
11protected voidshowUpdataDialog() { 
12    AlertDialog.Builder builer = newBuilder(this) ;  
13    builer.setTitle("版本升级"); 
14    builer.setMessage(info.getDescription()); 
15    //当点确定按钮时从服务器上下载 新的apk 然后安装   
16    builer.setPositiveButton("确定"newOnClickListener() { 
17    public void onClick(DialogInterface dialog, intwhich) { 
18            Log.i(TAG,"下载apk,更新"); 
19            downLoadApk(); 
20        }    
21    }); 
22    //当点取消按钮时进行登录  
23    builer.setNegativeButton("取消"newOnClickListener() { 
24        public void onClick(DialogInterface dialog, intwhich) { 
25            // TODO Auto-generated method stub  
26            LoginMain(); 
27        
28    }); 
29    AlertDialog dialog = builer.create(); 
30    dialog.show(); 
31}
01/*
02 * 从服务器中下载APK
03 */ 
04protected voiddownLoadApk() { 
05    finalProgressDialog pd;    //进度条对话框  
06    pd = new ProgressDialog(this); 
07    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
08    pd.setMessage("正在下载更新"); 
09    pd.show(); 
10    newThread(){ 
11        @Override 
12        public voidrun() { 
13            try
14                File file = DownLoadManager.getFileFromServer(info.getUrl(), pd); 
15                sleep(3000); 
16                installApk(file); 
17                pd.dismiss(); //结束掉进度条对话框  
18            catch(Exception e) { 
19                Message msg = newMessage(); 
20                msg.what = DOWN_ERROR; 
21                handler.sendMessage(msg); 
22                e.printStackTrace(); 
23            
24        }}.start(); 
25}
1//安装apk   
2protected voidinstallApk(File file) { 
3    Intent intent = newIntent(); 
4    //执行动作  
5    intent.setAction(Intent.ACTION_VIEW); 
6    //执行的数据类型  
7    intent.setDataAndType(Uri.fromFile(file), "application/vnd.Android.package-archive");//编者按:此处Android应为android,否则造成安装不了   
8    startActivity(intent); 
9}
1/*
2 * 进入程序的主界面
3 */ 
4private voidLoginMain(){ 
5    Intent intent = newIntent(this,MainActivity.class); 
6    startActivity(intent); 
7    //结束掉当前的activity   
8    this.finish(); 
9}

二、参考后使用情况:
1.可以下载apk,但安装失败:

1)以为配置文件中需定义了android.permission.INSTALL_PACKAGES,其实不需;

2)以为是要在执行安装的activity中配置

1<intent-filter>
2        <action android:name="android.intent.action.VIEW" />
3        <category android:name="android.intent.category.DEFAULT" />
4</intent-filter>

,其实不是; 

3)代码中application/vnd.Android.package-archive有一个字母A大写了,改小写后解决;

http://www.open-open.com/lib/view/open1339581191209.html

0 0
原创粉丝点击