Android 版本更新
来源:互联网 发布:知乎 陈廖宇 编辑:程序博客网 时间:2024/06/16 01:01
public class MainActivity extends AppCompatActivity {
private String json = "{\n" +
"\"versionCode\":2,\n" +
"\"versionName\":\"2.0\",\n" +
"\"desc\":\"最新的手机卫士,快来下载吧\", \"downloadUrl\":\"http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk\"\n" +
"}";
String version;//原来的版本号
String versionName;//最新版本号
String versionCode;//新版版本号的名字
String m_appNameStr; //下载到本地要给这个APP命的名字
Handler m_mainHandler;
ProgressDialog m_progressDlg;
String downloadUrl;
int aa = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_mainHandler = new Handler();
m_progressDlg = new ProgressDialog(this);
m_progressDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
m_progressDlg.setIndeterminate(false);
m_appNameStr = "haha.apk";
getVersion();
Log.i("versionName","当前系统的版本号:"+version);
try {
JSONObject jsonObject = new JSONObject(json);
versionName = jsonObject.getString("versionName");
versionCode = jsonObject.getString("versionCode");
downloadUrl = jsonObject.getString("downloadUrl");
Log.i("versionName:","最新的版本号:"+versionName);
if(TextUtils.equals(versionName,version)){
Log.i("versionName:","没有需要更新的版本!");
}else{
Log.i("versionName:","进入下载!");
doNewVersionUpdate();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void doNewVersionUpdate() {
String str= "当前版本:"+getVersion()+" Code:"+version+" ,发现新版本:"+versionName+
" Code:"+versionCode+" ,是否更新?";
Dialog dialog = new AlertDialog.Builder(this).setTitle("软件更新").setMessage(str)
// 设置内容
.setPositiveButton("更新",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
m_progressDlg.setTitle("正在下载");
m_progressDlg.setMessage("请稍候...");
downFile(downloadUrl);
}
})
.setNegativeButton("暂不更新",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 点击"取消"按钮之后退出程序
finish();
}
}).create();// 创建
// 显示对话框
dialog.show();
}
private void downFile(final String url)
{
m_progressDlg.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();
m_progressDlg.setMax((int)length);//设置进度条的最大值
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File(
Environment.getExternalStorageDirectory(),
m_appNameStr);
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
count += ch;
if (length > 0) {
m_progressDlg.setProgress(count);//设置当前进度
}
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
down(); //告诉HANDER已经下载完成了,可以安装了
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
//下载apk程序代码
protected File downLoadFile(String httpUrl) {
// TODO Auto-generated method stub
final String fileName = "updata.apk";
File tmpFile = new File("/sdcard/update");
if (!tmpFile.exists()) {
tmpFile.mkdir();
}
final File file = new File("/sdcard/update/" + fileName);
try {
URL url = new URL(httpUrl);
try {
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[256];
conn.connect();
double count = 0;
if (conn.getResponseCode() >= 400) {
Toast.makeText(MainActivity.this, "连接超时", Toast.LENGTH_SHORT)
.show();
} else {
while (count <= 100) {
if (is != null) {
int numRead = is.read(buf);
if (numRead <= 0) {
break;
} else {
fos.write(buf, 0, numRead);
}
} else {
break;
}
}
}
conn.disconnect();
fos.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}
/**
* 告诉HANDER已经下载完成了,可以安装了
*/
private void down() {
m_mainHandler.post(new Runnable() {
public void run() {
m_progressDlg.cancel();
update();
}
});
}
/**
* 安装程序
*/
void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(),m_appNameStr)),
"application/vnd.android.package-archive");
startActivity(intent);
}
public String getVersion(){
try {
PackageManager manager = this.getPackageManager();
PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
version = info.versionName;
return version;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return "应用当前的版本号:"+getPackageName();
}
}
}
要记得加这个依赖
android {
useLibrary 'org.apache.http.legacy'
}
useLibrary 'org.apache.http.legacy'
}
阅读全文
1 0
- Android版本更新
- Android SDK版本更新
- Android软件版本更新
- Android 检测版本更新
- Android之版本更新
- Android版本更新实现
- android版本更新问题
- android apk版本更新
- android版本更新
- Android 版本的更新
- Android版本更新
- Android提示版本更新
- android 版本更新
- Android应用版本更新
- android版本更新
- Android检测版本更新
- Android中版本更新
- Android 更新升级版本
- 内联函数与宏
- Spring中静态方法中使用@Resource注解的变量
- gapdebug problem loading url
- AndroidStudio Gradle构建项目报错:Error:The first result from the daemon was empty
- 分布式事务解决方案
- Android 版本更新
- SDSC 2017 Day 5 T3
- 在Tp5中使用SPL数据结构SplDoublyLinkedList(双向链表)
- request.getcontextPath() 详解
- CentOS7.3下安装Cloudera Manager5.11.1
- 2017易观A10峰会唤醒“源力” 将用户数据有价值地利用起来!
- Android折线图
- 漫画算法:最小栈的实现
- 【算法入门竞赛经典】【7.2枚举排列】