Android版本怎样更新,Android 版本自动更新

48304ba5e6f9fe08f3fa1abda7d326ab.png

package com.update.apk;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;

import org.json.JSONObject;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.Dialog;

import android.app.ProgressDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.pm.PackageManager.NameNotFoundException;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

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("

已是最新版本,无需更新");

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);

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要更新Android Studio的版本,可以按照以下步骤操作: 1. 打开Android Studio软件。 2. 点击菜单栏中的"Help"选项。 3. 从下拉菜单中选择"Check for Updates"。 4. 如果有可用的更新Android Studio会自动下载并安装最新版本。 5. 安装完成后,需要重启Android Studio才能生效。 需要注意的是,更新Android Studio的版本可能会导致某些插件或库不兼容,因此在更新前最好备份项目文件和设置。此外,为了保持项目的稳定性,建议在更新前先查看更新日志,了解更新的内容和影响。 ### 回答2: Android Studio 是由谷歌开发的一款用于开发Android应用程序的集成开发环境(IDE)。每隔一段时间,谷歌会发布新的Android Studio版本来提供更好的性能和功能。 更新Android Studio版本有以下几个原因: 1. 新功能和改进:每个新的Android Studio版本都会引入新的功能和改进,以提供更好的用户体验和开发效率。例如,新版本可能会增加对最新Android操作系统版本的支持,提供新的布局编辑器,改进性能分析工具等等。 2. Bug修复:每个软件都会存在一些bug和问题,通过升级到新版本可以修复已知问题并提高稳定性。新版本通常会修复一些老版本中存在的问题,并进行质量和稳定性改进。 3. 安全性更新:随着技术的发展,恶意软件和黑客攻击也在不断增加。通过更新Android Studio版本,可以获得最新的安全补丁和功能,以保护开发环境和应用程序的安全性。 4. 支持最新的开发工具和库:Android Studio是开发Android应用程序的首选工具,更新版本通常会提供对最新的开发工具和库的支持。这意味着开发者可以使用最新的API和功能来创建更先进,更具创新性的应用程序。 为了更新Android Studio版本,可以按照以下步骤进行: 1. 打开Android Studio,并转到“帮助”菜单。 2. 在下拉菜单中,选择“检查更新”选项。 3. Android Studio将会连接到服务器,检查是否有新版本可用。 4. 如果有新版本可用,Android Studio将提示您进行更新。点击“更新”按钮以开始下载和安装新版本。 5. 安装完成后,重新启动Android Studio以使更改生效。 总之,更新Android Studio版本是保持最新技术和功能的最佳方法,以提高开发效率和应用程序质量。 ### 回答3: Android Studio 是一个为 Android 应用开发提供的集成开发环境(IDE)。它的更新版本是为了修复之前版本的 bug、改进功能、增加新特性,从而使开发者能够更高效、更方便地开发 Android 应用程序。 每个新的 Android Studio 版本都会带来一些新的特性和改进。例如,最新版本Android Studio 4.0 引入了一种全新的布局编辑器,称为 Compose,它能够帮助开发者以更直观的方式创建用户界面。此外,它还增强了 Kotlin 语言的支持,包括新的代码模板和自动导入功能,以及一些性能优化,如更快的构建和启动时间。 更新 Android Studio 可以帮助开发者保持最新的开发环境,并利用新功能和改进来提高开发效率。更新版本通常会修复一些已知的 bug,并引入更好的性能和稳定性。此外,Android Studio 的更新版本通常会配合最新的 Android SDK 发布,提供对最新 Android 平台的开发支持,包括新的 API 和功能。 当我们更新 Android Studio 时,需要注意备份项目文件和配置,以防止数据丢失。同时,更新版本的安装可能需要一些时间和网络带宽,所以我们需要考虑在合适的时间进行更新。 总之,Android Studio 的更新版本是为了改进开发者的开发体验和开发效率,并提供对最新 Android 平台的支持。及时更新 Android Studio 可以使我们能够使用最新的开发工具和技术,保持竞争优势,并提供更好的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值