提交文件

This commit is contained in:
Your Name
2021-08-16 22:17:40 +08:00
commit 971c2e0e8a
26 changed files with 1270 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
package Interface;
public interface addVersionInterface {
//更新接口
public void fresh(String VersionId, String apkUrl, String updateDescription);
}

View File

@@ -0,0 +1,7 @@
package Interface;
public interface refreshInterFace {
//判断是否需要更新,比对是否有新的版本
public boolean new_version() throws Exception;
}

View File

@@ -0,0 +1,12 @@
package Service;
import Interface.addVersionInterface;
import dao.dataDao;
public class add implements addVersionInterface {
@Override
public void fresh(String VersionId, String apkUrl, String updateDescription) {
dataDao.add(VersionId, apkUrl, updateDescription);
}
}

View File

@@ -0,0 +1,42 @@
package Service;
import Interface.refreshInterFace;
import android.app.Application;
import android.content.Context;
import dao.dataDao;
import util.APKVersionCodeUtils;
public class refresh implements refreshInterFace {
//version1大返回一个正数小返回一个负数相等返回0
public static int compareVersion(String version1, String version2) throws Exception {
if (version1 == null || version2 == null) {
throw new Exception("compareVersion error:illegal params.");
}
String[] versionArray1 = version1.split("\\.");
String[] versionArray2 = version2.split("\\.");
int idx = 0;
int minLength = Math.min(versionArray1.length, versionArray2.length);
int diff = 0;
while (idx < minLength
&& (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0
&& (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {
++idx;
}
//如果已经分出大小,则直接返回,如果未分出大小,则再比较位数,有子版本的为大;
diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
return diff;
}
Application application = new Application();
Context ass = application.getApplicationContext();
String oldVersion = APKVersionCodeUtils.getVerName(ass);
String newVersion = dataDao.getNewVersion();
@Override
public boolean new_version() throws Exception {
if (compareVersion(oldVersion, newVersion) < 0){
return true;
}
return false;
}
}

View File

@@ -0,0 +1,51 @@
package dao;
import util.MysqlDriver;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class dataDao {
public static void add(String VersionId, String apkUrl, String updateDescription) {
Connection connection = MysqlDriver.getConnection();
String sql = "INSERT INTO `fresh` (`VersionId`,`apkUrl`,`updateDescription` ) VALUES(?,?,?)";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, VersionId);
preparedStatement.setString(2, apkUrl);
preparedStatement.setString(3, updateDescription);
preparedStatement.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
MysqlDriver.close(preparedStatement);
MysqlDriver.close(connection);
}
}
public static String getNewVersion(){
Connection connection = MysqlDriver.getConnection();
String sql = "SELECT VersionId FROM fresh ORDER BY id DESC LIMIT 1";
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String version = null;
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
version = resultSet.getString("VersionId");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
MysqlDriver.close(resultSet);
MysqlDriver.close(preparedStatement);
MysqlDriver.close(connection);
}
return version;
}
}

View File

@@ -0,0 +1,28 @@
package util;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
public class APKVersionCodeUtils {
/**
* 获取版本号名称
*
* @param context 上下文
* @return
*/
public static String getVerName(Context context) {
String verName = "";
try {
verName = context.getPackageManager().
getPackageInfo(context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return verName;
}
}

View File

@@ -0,0 +1,46 @@
package util;
import java.sql.*;
public class MysqlDriver {
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + "emoji" + "?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false", "root", "root");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement preparedStatement) {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}