Install APK
public class DownLoadApk { public static SharedPreferences sharedPrederences = null; //Start the installation interface public static void DownId(Context context, long downId){ DownloadManager mDownloadManager = (DownloadManager) (Context.DOWNLOAD_SERVICE); Uri downloadUri = (downId); startInstall(context, downloadUri); } /** * Jump to the installation interface * @param context scope * @param uri Package Name */ private static void startInstall(Context context, Uri uri) { Intent install = new Intent(Intent.ACTION_VIEW); (uri, "application/-archive"); (Intent.FLAG_ACTIVITY_NEW_TASK); (install); } //Delete the file public static boolean fileDelete(String filePath) { File file = new File(filePath); if (() == false) { return false; } return (); }
Send a request to get the input stream
Thread thread = new Thread() { @Override public void run() { (); //The address of XML stored in the ftp server String path = FileUtils.getDevice_address()+""; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); ("GET"); (5000); (5000); //Send http GET request and get the corresponding code if (() == 200) { InputStream is = (); // Use the pull parser to start parsing this stream parseNewsXml(is); } } catch (Exception e) { // TODO Auto-generated catch block (); } } }; ();
Parse XML files
private void parseNewsXml(InputStream is) { XmlPullParser xp = (); try { (is, "utf-8"); //Judge the event type of the node and you can know what node the current node is int type = (); News news = null; while (type != XmlPullParser.END_DOCUMENT) { switch (type) { case XmlPullParser.START_TAG: if ("newslist".equals(())) { newsList = new ArrayList<>(); break; } else if ("news".equals(())) { news = new News(); break; } else if ("name".equals(())) { String name = (); (name); break; } else if ("code".equals(())) { String code = (); (code); break; } case XmlPullParser.END_TAG: if ("news".equals(())) { (news); } break; default: break; } //After parsing the current node, move the pointer to the next node until the node is finished and return its event type type = (); } // Send a message (1); } catch (Exception e) { (); } }
You can start downloading
//Get the download managerDownloadManager manager =(DownloadManager)(mContext.DOWNLOAD_SERVICE); handler = new Handler() { @Override public void handleMessage(Message msg) { (msg); News news = (0); ("aii", "XML: "+()+",apk:"+getPackageInfo(mContext)); if((())>(getPackageInfo(mContext))){ if(dowmCliek) { //Open the progress bar thread isRun = true; dowmCliek = false; //Delete the original installation package before updating the APK (path + "/" + mAPK); //Create a download request down = new ( (mWebsite)); //Set the allowed network type, here are both mobile network and wifi (.NETWORK_MOBILE | .NETWORK_WIFI); //Not issued notifications, both background downloads (true); //Do not display the download interface (true); //title (mContext, null, "XXX is upgrading..."); //Put the download request into the queue and return the download id downId = (down); }else{ (mContext,"Upgrading...",Toast.LENGTH_SHORT).show(); } }else{ (mContext,"It's the latest version, no need to upgrade...",Toast.LENGTH_SHORT).show(); } } };
Track the download progress
//Timed tasksScheduledExecutorService scheduledExecutorService = (1); (new Runnable() { @Override public void run() { if(isRun) { Message msg = (); = 1; (msg); } } }, 0, 100, );//Delay 0, interval 100, unit millisecondsprivate Handler mHandler = new Handler(new () { @Override public boolean handleMessage(Message msg) { switch () { case 1: //android download manager query = new ().setFilterById(downId); Cursor cursor = (query); if (cursor != null && ()) { //Search the file size directly here long downSize = (( DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); //Get the total file download size fileTotalSize =(( DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); (); ("Print", "Total Size" + downSize); ("Print", "Download Progress" + fileTotalSize); if (fileTotalSize>0) { NumberFormat numberFormat = (); (2); String result = ((float)fileTotalSize/(float)downSize*100); ("Print", "downloaded size: " + result+"%"); (result+"%"); } //Downloaded if(fileTotalSize==downSize) { isRun = false; ("Click to upgrade"); } } } return true; } });
After downloading, start the installation
DownloadCompleteReceiver receiver = new DownloadCompleteReceiver(); //Broadcast after downloadingclass DownloadCompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){ long downId = (DownloadManager.EXTRA_DOWNLOAD_ID, -1); if(downId!=-1) { //Start the installation (context,downId); dowmCliek=true; } }else{ (context, ()+"Download failed", Toast.LENGTH_SHORT).show(); } } } //Start download and complete broadcast(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Get the project package name
private static String getPackageInfo(Context context) { PackageInfo pi = null; try { PackageManager pm = (); pi = ((), PackageManager.GET_CONFIGURATIONS); return +""; } catch (Exception e) { (); } return null; }
Summarize
The above is the method of Android parsing XML files upgrade APK introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!