1.获取版本信息
public static String getVersionName(Context context) {
String versionName;
// 获取包管理器
PackageManager packageManager = context.getPackageManager();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionName = packageInfo.versionName;
} catch (NameNotFoundException e) {
versionName = "获取版本信息错误";
e.printStackTrace();
}
return versionName;
}
2.获取服务器版本信息
class GetServerVisionRunnable implements Runnable {
@Override
public void run() {
InputStream is = null;
BufferedReader br = null;
try {
// 创建连接
HttpURLConnection connection = (HttpURLConnection) new URL(
"http://192.168.129.102:8080/version.json").openConnection();
// 设置连接超时时间
connection.setConnectTimeout(5000);
// 设置读取超时时间
connection.setReadTimeout(5000);
// 连接
connection.connect();
String result = "";
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String readlline;
while ((readlline = br.readLine()) != null) {
result += readlline;
}
JSONObject json = new JSONObject(result);
int serverVersionCode = json.optInt("versioncode");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
CommonUtils.CloseStream(is);
CommonUtils.CloseStream(br);
}
}
}
3.下载、安装apk
class DownloadRunnable implements Runnable {
private InputStream inputStream;
private FileOutputStream fos;
private ProgressDialog pd;
public DownloadRunnable(ProgressDialog pd) {
this.pd = pd;
}
@Override
public void run() {
try {
// 初始化
HttpURLConnection connection =
(HttpURLConnection) new URL(downUrl).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
// 连接成功
if (connection.getResponseCode() == 200) {
// 创建文件
File file = new File(Environment.getExternalStorageDirectory(), "test.apk");
inputStream = connection.getInputStream();
// 获取文件总长度
int totalLength = connection.getContentLength();
// 设置进度条的总大小
pd.setMax(totalLength);
fos = new FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
int current = 0;
while ((len = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
current += len;
// 设置进度条的进度
pd.setProgress(current);
}
// 隐藏进度条
pd.dismiss();
// 安装应用
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri.parse("file:" + file.getAbsolutePath()),
"application/vnd.android.package-archive");
startActivityForResult(intent, REQ_CODE_INSTALL_APP);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
CommonUtils.CloseStream(inputStream);
CommonUtils.CloseStream(fos);
}
}
}
4.动画
//沿Y轴旋转的属性动画
ObjectAnimator animator = ObjectAnimator.ofFloat(iv_icon, "rotationY", 0, 45, 90, 135, 180, 225, 270, 315, 360);
// 时常
animator.setDuration(2000);
// 重复次数
animator.setRepeatCount(ObjectAnimator.INFINITE);
// 重复模式
animator.setRepeatMode(ObjectAnimator.REVERSE);
animator.start();
5.按钮按压效果选择器
在res/drawable/目录下新建一个正常状态下的shape