android自动更新新版模块(简单,实用)
来源:互联网 发布:程序员空闲 编辑:程序博客网 时间:2024/04/30 23:47
http://blog.csdn.net/hytfly/article/details/8549858
每一个好的android应用都得有自动更新的模块
直接上代码了,非常简单。
- public class MainActivity extends Activity {
- /** Called when the activity is first created. */
- String newVerName = "";//新版本名称
- int newVerCode = -1;//新版本号
- ProgressDialog pd = null;
- String UPDATE_SERVERAPK = "ApkUpdateAndroid.apk";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- if(getServerVer()){
- int verCode = this.getVerCode(this);
- if(newVerCode>verCode){
- doNewVersionUpdate();//更新版本
- }else{
- notNewVersionUpdate();//提示已是最新版本
- }
- }
- }
- /**
- * 获得版本号
- */
- public int getVerCode(Context context){
- int verCode = -1;
- try {
- verCode = context.getPackageManager().getPackageInfo("com.update.apk", 0).versionCode;
- } catch (NameNotFoundException e) {
- // TODO Auto-generated catch block
- Log.e("版本号获取异常", e.getMessage());
- }
- return verCode;
- }
- /**
- * 获得版本名称
- */
- public String getVerName(Context context){
- String verName = "";
- try {
- verName = context.getPackageManager().getPackageInfo("com.update.apk", 0).versionName;
- } catch (NameNotFoundException e) {
- Log.e("版本名称获取异常", e.getMessage());
- }
- return verName;
- }
- /**
- * 从服务器端获得版本号与版本名称
- * @return
- */
- public boolean getServerVer(){
- try {
- URL url = new URL("http://10.0.2.2:8080/ApkUpdateService/ver");
- HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
- httpConnection.setDoInput(true);
- httpConnection.setDoOutput(true);
- httpConnection.setRequestMethod("GET");
- httpConnection.connect();
- InputStreamReader reader = new InputStreamReader(httpConnection.getInputStream());
- BufferedReader bReader = new BufferedReader(reader);
- String json = bReader.readLine();
- JSONArray array = new JSONArray(json);
- JSONObject jsonObj = array.getJSONObject(0);
- newVerCode = Integer.parseInt(jsonObj.getString("verCode"));
- newVerName = jsonObj.getString("verName");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- }
- return true;
- }
- /**
- * 不更新版本
- */
- public void notNewVersionUpdate(){
- int verCode = this.getVerCode(this);
- String verName = this.getVerName(this);
- StringBuffer sb = new StringBuffer();
- sb.append("当前版本:");
- sb.append(verName);
- sb.append(" Code:");
- sb.append(verCode);
- sb.append("\n已是最新版本,无需更新");
- Dialog dialog = new AlertDialog.Builder(this)
- .setTitle("软件更新")
- .setMessage(sb.toString())
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- finish();
- }
- }).create();
- dialog.show();
- }
- /**
- * 更新版本
- */
- public void doNewVersionUpdate(){
- int verCode = this.getVerCode(this);
- String verName = this.getVerName(this);
- StringBuffer sb = new StringBuffer();
- sb.append("当前版本:");
- sb.append(verName);
- sb.append(" Code:");
- sb.append(verCode);
- sb.append(",发现版本:");
- sb.append(newVerName);
- sb.append(" Code:");
- sb.append(verCode);
- sb.append(",是否更新");
- Dialog dialog = new AlertDialog.Builder(this)
- .setTitle("软件更新")
- .setMessage(sb.toString())
- .setPositiveButton("更新", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- pd = new ProgressDialog(MainActivity.this);
- pd.setTitle("正在下载");
- pd.setMessage("请稍后。。。");
- pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- downFile("http://10.0.2.2:8080/ApkUpdateService/ApkUpdateAndroid.apk");
- }
- })
- .setNegativeButton("暂不更新", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- finish();
- }
- }).create();
- //显示更新框
- dialog.show();
- }
- /**
- * 下载apk
- */
- public void downFile(final String url){
- pd.show();
- 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();
- long length = entity.getContentLength();
- InputStream is = entity.getContent();
- FileOutputStream fileOutputStream = null;
- if(is != null){
- File file = new File(Environment.getExternalStorageDirectory(),UPDATE_SERVERAPK);
- fileOutputStream = new FileOutputStream(file);
- byte[] b = new byte[1024];
- int charb = -1;
- int count = 0;
- while((charb = is.read(b))!=-1){
- fileOutputStream.write(b, 0, charb);
- count += charb;
- }
- }
- fileOutputStream.flush();
- if(fileOutputStream!=null){
- fileOutputStream.close();
- }
- down();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }.start();
- }
- Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- pd.cancel();
- update();
- }
- };
- /**
- * 下载完成,通过handler将下载对话框取消
- */
- public void down(){
- new Thread(){
- public void run(){
- Message message = handler.obtainMessage();
- handler.sendMessage(message);
- }
- }.start();
- }
- /**
- * 安装应用
- */
- public void update(){
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(),UPDATE_SERVERAPK))
- , "application/vnd.android.package-archive");
- startActivity(intent);
- }
- }
0 0
- android自动更新新版模块(简单,实用)
- android自动更新新版模块(简单,实用)
- 小巧实用的android自动更新模块代码
- Android应用的自动更新模块
- Android应用的自动更新模块
- Android应用的自动更新模块
- Android应用的自动更新模块
- android项目的自动更新模块
- Python logging 模块简单实用
- android自动更新模块实现中知识总结
- Android实现应用自动更新的方法模块
- android (PopupWindow)简单实用实例
- ReactNative打包-Android(简单实用)
- Android---RecyclerView简单实用(上)
- android-butterknife 简单实用
- 【Android 应用开发】Android应用的自动更新模块
- 简单实用的ASP用户登陆模块
- python中的sockeserver模块简单实用
- CSS3+SVG 响应式图标设计
- PHPCMS V9上传附件图片出现“服务器安全认证错误”解决方法
- array数组‘查找’操作
- Linux查看连接数,并发数
- 黑马程序员Objective-C笔记:点语法
- android自动更新新版模块(简单,实用)
- 齐次坐标的理解
- C#面向对象第二天总结
- android 多渠道打包工具 可打包libs和.so文件
- eclipse不能自动编译APK
- python : 将txt文件中的数据读为numpy数组或列表
- Leetcode: Intersection of Two Linked Lists
- apache性能调优
- schema.doc.xml报错pic: org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Coul