SoFunction
Updated on 2025-04-11

Android version detection Introduction to Android program version detection and update implementation


package android.***;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class UpdateVer extends Activity{
private static final String TAG = "DOWNLOADAPK";
private String PastVersion;
private String NowVersion;
public ProgressDialog pBar;
private String currentFilePath = "";
private String fileEx="";
private String fileNa="";
private String strURL="";
private String VersionUri ="";
private Context mContext;
private final String fileVer = "";
public UpdateVer(String urlapk,String urlver,final Context context){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String ver = "?ver=" + (new Date());// Mainly avoiding the cache of the mobile phone
strURL = urlapk + ver;
VersionUri = urlver + ver;
mContext = context;
}
public void checkVer() {
// parse the Version web page and get the version number
getVersionxml(VersionUri);
}
private void compareVer() {
load();

//When there is a newest version
if(PastVersion != null && !(NowVersion)){
Dialog dialog = new (mContext).setTitle("System Update")
.setMessage(("Discover the new version %s, the current version is %s, please update!",NowVersion,PastVersion))// Set content
// Set the OK button
.setPositiveButton("OK"
,new () {
@Override
public void onClick(DialogInterface dialog,
int which) {
pBar = new ProgressDialog(mContext);
("Downloading");
("Please wait...");
(ProgressDialog.STYLE_SPINNER);
fileEx = ((".") + 1,()).toLowerCase();
fileEx = (0,("?"));
fileNa = (("/") + 1,("."));
getFile(strURL);
}
}).setNegativeButton("Cancel",
new () {
@Override
public void onClick(DialogInterface dialog,
int whichButton) {
// Click the "Cancel" button to exit the program
}
}).create();// Create
// Show dialog box
();
}
else{
(mContext, ("currently latest version %s",PastVersion), Toast.LENGTH_LONG).show();
}
}
private void getFile(final String strPath)
{
();
try{
if ((currentFilePath) ){
getDataSource(strPath);
}
currentFilePath = strPath;
Runnable r = new Runnable(){
@Override
public void run()
{
try{
getDataSource(strPath);
}
catch (Exception e){
(TAG, (), e);
}
}
};
new Thread(r).start();
}
catch(Exception e){
();
}
}
/* Obtain remote file*/
private void getDataSource(String strPath) throws Exception {
if (!(strPath)) {
("Tag","error");
}
else {
/*Get URL*/
URL myURL = new URL(strPath);
/*Create online*/
URLConnection conn = ();
();
/*InputStream Download file*/
InputStream is = ();
if (is == null) {
("tag","error");
throw new RuntimeException("No file content was read");
}
/*Create temporary files*/
File myTempFile = (fileNa, "." + fileEx);
();
/*Write the file to the temporary disk*/
FileOutputStream fos = new FileOutputStream(myTempFile);
byte buf[] = new byte[128];
do{
int numread = (buf);
if (numread <= 0) {
break;
}
(buf, 0, numread);
}while (true);

/*Open the file for installation*/
openFile(myTempFile);
try {
();
}
catch (Exception ex){
("Tag","error");
(TAG, "error: " + (), ex);
}
}
}
/* Open file on your phone */
private void openFile(File f) {
();
Intent intent = new Intent();
(Intent.FLAG_ACTIVITY_NEW_TASK);
(.ACTION_VIEW);
/* Call getMIMEType() to get MimeType */
String type = getMIMEType(f);
/* Set the file and MimeType of the intent */
((f),type);
(intent);
}
/* determine the method of the file MimeType */
private String getMIMEType(File f) {
String type = "";
String fName = ();
/* Get the extension */
String end = ((".")+1,()).toLowerCase();

/* Determine MimeType by the type of extension */
if(("m4a")
|| ("mp3")
|| ("mid")
|| ("xmf")
|| ("ogg")
|| ("wav")){
type = "audio";
}
else if(("3gp") || ("mp4")){
type = "video";
}
else if(("jpg")
|| ("gif")
|| ("png")
|| ("jpeg")
|| ("bmp")){
type = "image";
}
else if(("apk")){
/* .INSTALL_PACKAGES */
type = "application/-archive";
}
else{
type = "*";
}
/*If it cannot be opened directly, the software list will pop up for the user to choose */
if(!("apk")){
type += "/*";
}
return type;
}
private void getVersionxml(String resourceUrl){
GetVer gv = new GetVer();
(resourceUrl);
}
private boolean load(){
Properties properties = new Properties();
try{
InputStream stream = ().open(fileVer);
//FileInputStream stream = (fileVer);
//Read the file content
(stream);
}
catch (FileNotFoundException e){
return false;
}
catch(IOException e){
return false;
}
catch(Exception e){
return false;
}
PastVersion = (("Version").toString());
return true;
}

//==========================================================================
// GetVer
//==========================================================================
class GetVer extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... urlVer) {
String db = null;
URL url = null;

try {
url = new URL(urlVer[0]);
}
catch (MalformedURLException e) {
();
}
InputSource is = null;
try {
is = new InputSource(());
("UTF-8");
db = (is);
}
catch (Exception e) {
();
}
return db;
}
@Override
protected void onCancelled() {
();
}
@Override
protected void onPostExecute(String result) {
NowVersion = result;
compareVer();
}
}
}