您的位置:首页 > 其它

版本更新

2016-07-27 15:39 274 查看
public class MainActivity extends AppCompatActivity {
Info info;
Handler mhandler=new Handler(){
@Override
public void handleMessage(Message msg) {
String data= (String) msg.obj;
Gson gson=new Gson();
info= gson.fromJson(data, Info.class);
if(getVersion()!=0){
<span style="white-space:pre">	</span><span style="color:#ff0000;">//最新版本与当前版本比较</span>
if(info.getVerCode()>getVersion()){
Toast.makeText(MainActivity.this,"开始下载",Toast.LENGTH_SHORT).show();
String path=info.getApkpath();
<span style="white-space:pre">		</span><span style="color:#ff0000;">//如果需要更新则下载apk</span>
update(path, Environment.getExternalStorageDirectory().getPath()+"/update.apk");
}else{
Toast.makeText(MainActivity.this,"当前已是最新版本,无需更新",Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this,"获取版本号失败",Toast.LENGTH_SHORT).show();
}

}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getData("http://169.254.245.7:8080/MyServer/servlet/CheckUpdateServlet");
}
<span style="white-space:pre">	</span><span style="color:#ff0000;">//网络请求,获得最新版本</span>
private void getData(String path) {
RequestParams params = new RequestParams();

<span style="white-space:pre">	</span>
HttpUtils httpUtils = new HttpUtils();
//        // 配置当前网络缓存到期时间 (0秒内数据那缓存数据)默认60s
//        httpUtils.configCurrentHttpCacheExpiry(0);
// 开始连接,RequestCallBack<T>是得到的数据类型
httpUtils.send(HttpRequest.HttpMethod.GET, path,params, new RequestCallBack<String>() {

@Override
public void onFailure(HttpException arg0, String arg1) {
Log.i("info","0000000000000000000000");
}

@Override
public void onSuccess(ResponseInfo<String> arg0) {
Message msg=Message.obtain();
msg.obj=arg0.result;
mhandler.sendMessage(msg);
}
});
}
<span style="color:#ff0000;">//获得当前版本的方法</span>
public int getVersion(){
PackageManager manager = this.getPackageManager();
int versionCode=0;
try {
PackageInfo info = manager.getPackageInfo(this.getPackageName(),0);
versionCode = info.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
<span style="color:#ff0000;">//下载apk的方法,url为下载网址,path为下载路径</span>
public void update(String url,String path){
HttpUtils hu=new HttpUtils();
hu.download(url, path, new RequestCallBack<File>() {
@Override
public void onLoading(long total, long current, boolean isUploading) {
Log.i("info",current+"/"+total);
super.onLoading(total, current, isUploading);
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
<span style="color:#ff6666;">//下载完成后弹出通知</span>
Intent resultIntent = new Intent(MainActivity.this, ResultActivity.class);

///// 第一步:获取NotificationManager
NotificationManager nm = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);

///// 第二步:定义Notification
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("info",info);
//PendingIntent是待执行的Intent
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("title")
.setContentText("text")
.setSmallIcon(R.drawable.push).setContentIntent(pi)
.build();
notification.flags = Notification.FLAG_NO_CLEAR;

/////第三步:启动通知栏,第一个参数是一个通知的唯一标识
nm.notify(0, notification);
}
@Override
public void onFailure(HttpException e, String s) {
Toast.makeText(MainActivity.this,"下载失败",Toast.LENGTH_SHORT).show();
}
});
}
}


//安装apk的方法

public void update(){
File apkfile = new File(
<span style="color:#ff0000;">Environment.getExternalStorageDirectory()+"/update.apk"</span>);//apk路径
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkfile),
"application/vnd.android.package-archive");
startActivity(intent);
}


//用httpurlconnection下载

从服务器下载apk:
public static File getFileFromServer(String path, ProgressDialog pd) throws Exception{
//如果相等的话表示当前的sdcard挂载在手机上并且是可用的
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
URL url = new URL(path);
HttpURLConnection conn =  (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
//获取到文件的大小
pd.setMax(conn.getContentLength());
InputStream is = conn.getInputStream();
File file = new File(Environment.getExternalStorageDirectory(), "updata.apk");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len ;
int total=0;
while((len =bis.read(buffer))!=-1){
fos.write(buffer, 0, len);
total+= len;
//获取当前下载量
pd.setProgress(total);
}
fos.close();
bis.close();
is.close();
return file;
}
else{
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: