package com.ljp.version;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
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.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.ljp.version.service.VersionService;
public class TestVersionUpdateActivity extends ActivityGroup {
private int currentV=0, newV=0;
private JSONObject jo_v;
private String newVContent = "";
private ProgressBar pb;private TextView tv;
public static int loading_process;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
loading_process=0;
if (isConnect(TestVersionUpdateActivity.this)) {
// TODO Auto-generated method stub
currentV = getVerCode(TestVersionUpdateActivity.this,"com.ljp.version");
new Thread() {
public void run() {
jo_v = getJsonObject("http://1.nightman.sinaapp.com/test/newVersion.php");
if (jo_v != null && jo_v.has("version"))
try {
newV = jo_v.getInt("version");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (newV > currentV) {
Message msg = BroadcastHandler.obtainMessage();
BroadcastHandler.sendMessage(msg);
}
}
}.start();
}
}
private Handler BroadcastHandler = new Handler() {
public void handleMessage(Message msg) {
if (jo_v.has("content")) {
JSONArray items;
try {
items = jo_v.getJSONArray("content");
for (int i = 0; i < items.length(); i++) {
JSONObject d = items.getJSONObject(i);
newVContent = newVContent + (i + 1) + "." + d.getString("text")
+ "\n";
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Dialog dialog = new AlertDialog.Builder(
TestVersionUpdateActivity.this)
.setTitle("新版本更新内容")
.setMessage(newVContent)
.setPositiveButton("马上更新",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Beginning();
dialog.dismiss();
}
})
.setNegativeButton("以后再说",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.dismiss();
}
}).create();
dialog.show();
}
};
public void Beginning(){
LinearLayout ll = (LinearLayout) LayoutInflater.from(TestVersionUpdateActivity.this).inflate(
R.layout.layout_loadapk, null);
pb = (ProgressBar) ll.findViewById(R.id.down_pb);
tv = (TextView) ll.findViewById(R.id.tv);
Builder builder = new Builder(TestVersionUpdateActivity.this);
builder.setView(ll);builder.setTitle("版本更新进度提示");
builder.setNegativeButton("后台下载",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(TestVersionUpdateActivity.this, VersionService.class);
startService(intent);
dialog.dismiss();
}
});
builder.show();
new Thread() {
public void run() {
loadFile("http://1.nightman.sinaapp.com/test/good.zip");
}
}.start();
}
public void loadFile(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
float length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File(Environment.getExternalStorageDirectory(),
"NightMan.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
float count = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
count += ch;
sendMsg(1,(int) (count*100/length));
}
}
sendMsg(2,0);
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (Exception e) {
sendMsg(-1,0);
}
}
private void sendMsg(int flag,int c) {
Message msg = new Message();
msg.what = flag;msg.arg1=c;
handler.sendMessage(msg);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {// 定义一个Handler,用于处理下载线程与UI间通讯
if (!Thread.currentThread().isInterrupted()) {
switch (msg.what) {
case 1:
pb.setProgress(msg.arg1);
loading_process = msg.arg1;
tv.setText("已为您加载了:" + loading_process + "%");
break;
case 2:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "NightMan.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
break;
case -1:
String error = msg.getData().getString("error");
Toast.makeText(TestVersionUpdateActivity.this, error, 1).show();
break;
}
}
super.handleMessage(msg);
}
};
public JSONObject getJsonObject(String Url) {
HttpClient client = new DefaultHttpClient();
StringBuilder sb = new StringBuilder();
String js = null;JSONObject son=null;
HttpGet myget = new HttpGet(Url);
try {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 8000);
HttpResponse response = client.execute(myget);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
sb.append(s);
}
js = sb.toString();
son = new JSONObject(js);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("异常-》下载转化JSON");
return null;
}
return son;
}
public boolean isConnect(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
public int getVerCode(Context _context,String _package) {
int verCode = -1;
try {
verCode = _context.getPackageManager().getPackageInfo(
_package, 0).versionCode;
} catch (NameNotFoundException e) {
}
return verCode;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android版本升级demo
共38个文件
class:17个
png:6个
xml:5个
需积分: 9 54 下载量 48 浏览量
2014-07-10
18:53:20
上传
评论
收藏 90KB ZIP 举报
温馨提示
一个强大实用的版本升级demo(thread+service+Notification) 服务器关闭 地址需要自己重新替换成别的URL
资源推荐
资源详情
资源评论
收起资源包目录
versionUpdate.zip (38个子文件)
versionUpdate
TestVersionUpdate
bin
classes.dex 15KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
TestVersionUpdate.apk 22KB
classes
com
ljp
version
R$layout.class 474B
R$id.class 497B
R$drawable.class 398B
TestVersionUpdateActivity$1$1.class 1KB
TestVersionUpdateActivity$4.class 1KB
R$attr.class 331B
R.class 510B
TestVersionUpdateActivity$1$2.class 976B
TestVersionUpdateActivity$3.class 2KB
TestVersionUpdateActivity$2.class 3KB
R$string.class 418B
TestVersionUpdateActivity$1.class 3KB
service
VersionService$1$1.class 1KB
VersionService$1.class 1KB
VersionService.class 4KB
TestVersionUpdateActivity$5.class 760B
TestVersionUpdateActivity.class 9KB
resources.ap_ 12KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
values
strings.xml 200B
drawable-mdpi
ic_launcher.png 3KB
layout
notification_version.xml 2KB
layout_loadapk.xml 649B
layout_main.xml 398B
assets
gen
com
ljp
version
R.java 1KB
proguard.cfg 1KB
src
com
ljp
version
service
VersionService.java 3KB
TestVersionUpdateActivity.java 8KB
.project 853B
.classpath 364B
project.properties 360B
AndroidManifest.xml 1KB
共 38 条
- 1
资源评论
Flutter鸿蒙梁典典学院
- 粉丝: 476
- 资源: 221
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功