Due to your comments, I have made some modifications, made some comments, and updated them. I hope you can provide more support. Thank you!
Package com. autoupdate;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. InputStream;
Import java.net. URL;
Import java.net. URLConnection;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. ProgressDialog;
Import android. content. Context;
Import android. content. DialogInterface;
Import android. content. Intent;
Import android. content. pm. PackageInfo;
Import android. content. pm. PackageManager. NameNotFoundException;
Import android.net. ConnectivityManager;
Import android.net. NetworkInfo;
Import android.net. Uri;
Import android. util. Log;
Import android. webkit. URLUtil;
Import com. AutoUpdate. R;
/**
* Version check and automatic update
*
* @ Author shenyj-ydrh 1. Detect updates through URL 2. Download and install updates 3. Delete temporary paths
*
*/
Public class myautoupdate {
// Call the updated activity
Public activity = NULL;
// Current version number
Public int versioncode = 0;
// Name of the current version
Public String versionname = "";
// Console information identifier
Private Static final string tag = "AutoUpdate ";
// Current file path
Private string currentfilepath = "";
// Temporary path of the installation package file
Private string currenttempfilepath = "";
// Obtain the file extension string
Private string fileex = "";
// Obtain the file name string
Private string filena = "";
// Server address
Private string strurl = "http: // 127.0.0.1: 8080/apidemos.apk ";
Private ProgressDialog dialog;
/**
* Constructor to obtain the current version information
*
* @ Param activity
*/
Public MyAutoUpdate (Activity activity ){
This. activity = activity;
// Obtain the current version
GetCurrentVersion ();
}
/**
* Update Detection
*/
Public void check (){
// Detect the network
If (isNetworkAvailable (this. activity) = false ){
Return;
}
// If the Network is available, the new version is detected.
If (true ){
// In the displayed dialog box, select whether to update the version.
ShowUpdateDialog ();
}
}
/**
* Check whether a network is available
*
* @ Param context
* @ Return network connection status
*/
Public static boolean isNetworkAvailable (Context context ){
Try {
ConnectivityManager cm = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
// Obtain Network Information
NetworkInfo info = cm. getActiveNetworkInfo ();
// Return the detected network status
Return (info! = Null & info. isConnected ());
} Catch (Exception e ){
E. printStackTrace ();
Return false;
}
}
/**
* In the dialog box that appears, select whether to update the version.
*/
Public void showUpdateDialog (){
@ SuppressWarnings ("unused ")
AlertDialog alert = new AlertDialog. Builder (this. activity)
. SetTitle ("new version"). setIcon (R. drawable. ic_launcher)
. SetMessage ("Update? ")
. SetPositiveButton ("yes", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){
// Download an object through the address
DownloadTheFile (strURL );
// Display the update status and progress bar
ShowWaitDialog ();
}
})
. SetNegativeButton ("no", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){
Dialog. cancel ();
}
}). Show ();
}
/**
* Display the update status and progress bar
*/
Public void showWaitDialog (){
Dialog = new ProgressDialog (activity );
Dialog. setMessage ("Updating ...");
Dialog. setIndeterminate (true );
Dialog. setCancelable (true );
Dialog. show ();
}
/**
* Obtain the current version information.
*/
Public void getCurrentVersion (){
Try {
// Obtain the application package information
PackageInfo info = activity. getPackageManager (). getPackageInfo (
Activity. getPackageName (), 0 );
This. versionCode = info. versionCode;
This. versionName = info. versionName;
} Catch (NameNotFoundException e ){
E. printStackTrace ();
}
}
/**
* Truncate the file name and download it.
*
* @ Param strPath
*/
Private void downloadTheFile (final String strPath ){
// Obtain the file extension string
FileEx = strURL. substring (strURL. lastIndexOf (".") + 1, strURL. length ())
. ToLowerCase ();
// Obtain the file name string
FileNa = strURL. substring (strURL. lastIndexOf ("/") + 1,
StrURL. lastIndexOf ("."));
Try {
If (strPath. equals (currentFilePath )){
DoDownloadTheFile (strPath );
}
CurrentFilePath = strPath;
New Thread (new Runnable (){
@ Override
Public void run (){
// TODO Auto-generated method stub
Try {
// Execute the download
DoDownloadTheFile (strPath );
} Catch (Exception e ){
Log. e (TAG, e. getMessage (), e );
}
}
}). Start ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
/**
* Download and install the new version.
*
* @ Param strPath
* @ Throws Exception
*/
Private void doDownloadTheFile (String strPath) throws Exception {
Log. I (TAG, "getDataSource ()");
// Determine whether strPath is a network address
If (! URLUtil. isNetworkUrl (strPath )){
Log. I (TAG, "server address error! ");
} Else {
URL myURL = new URL (strPath );
URLConnection conn = myURL. openConnection ();
Conn. connect ();
InputStream is = conn. getInputStream ();
If (is = null ){
Throw new RuntimeException ("stream is null ");
}
// Generate a temporary file
File myTempFile = File. createTempFile (fileNa, "." + fileEx );
// Temporary path of the installation package file
CurrentTempFilePath = myTempFile. getAbsolutePath ();
FileOutputStream fos = new FileOutputStream (myTempFile );
Byte buf [] = new byte [128];
Do {
Int numread = is. read (buf );
If (numread <= 0 ){
Break;
}
Fos. write (buf, 0, numread );
} While (true );
Log. I (TAG, "getdatasource () download OK ...");
Dialog. Cancel ();
Dialog. Dismiss ();
// Open the file
Openfile (mytempfile );
Try {
Is. Close ();
} Catch (exception ex ){
Log. E (TAG, "getdatasource () error:" + ex. getmessage (), Ex );
}
}
}
/**
* Open a file for Installation
*
* @ Param F
*/
Private void openfile (file F ){
Intent intent = new intent ();
Intent. addflags (intent. flag_activity_new_task );
Intent. setaction (Android. content. Intent. action_view );
// Obtain the downloaded file type
String type = getmimetype (f );
// Open various types of files
Intent. setdataandtype (URI. fromfile (F), type );
// Install
Activity. startActivity (intent );
}
/**
* Delete the installation package in the temporary path
*/
Public void delFile (){
Log. I (TAG, "The TempFile (" + currentTempFilePath + ") was deleted .");
File myFile = new File (currentTempFilePath );
If (myFile. exists ()){
MyFile. delete ();
}
}
/**
* Get the type of the downloaded object
*
* @ Param f
* File name
* @ Return file type
*/
Private String getMIMEType (File f ){
String type = "";
// Obtain the file name
String fName = f. getName ();
// Obtain the file extension
String end = fName
. Substring (fName. lastIndexOf (".") + 1, fName. length ())
. ToLowerCase ();
If (end. Equals ("m4a") | end. Equals ("MP3") | end. Equals ("mid ")
| End. Equals ("xmf") | end. Equals ("Ogg") | end. Equals ("WAV ")){
Type = "audio ";
} Else if (end. Equals ("3GP") | end. Equals ("MP4 ")){
Type = "video ";
} Else if (end. Equals ("jpg") | end. Equals ("GIF") | end. Equals ("PNG ")
| End. Equals ("Jpeg") | end. Equals ("BMP ")){
Type = "image ";
} Else if (end. Equals ("APK ")){
Type = "application/vnd. Android. Package-Archive ";
} Else {
Type = "*";
}
If (end. Equals ("APK ")){
} Else {
Type + = "/*";
}
Return type;
}
}