Android software version update

Source: Internet
Author: User

public class Mainactivity extends Activity {
private static final String download_address = "http://10.0.2.2:8080/test1118/weishi360.apk";
private static final String check_address = "Http://10.0.2.2:8080/test1118/version.xml";
private static final String package_name = "Com.ruicaiedu.day_1224_updatesoftware";

Private Alertdialog Dialog;
Private ProgressBar PB;
Private ProgressBar pb_download;
Private APK apk;

Private String oldversion;
Private String newversion;

Private Handler Handler = new Handler () {
@Override
public void Handlemessage (Message msg) {
Switch (msg.what) {
Case 1://has a new version
APK = (apk) msg.obj;
NewVersion = Apk.getversionname ();
Oldversion = Getversionname ();
if (!newversion.equals (oldversion)) {
ShowDialog ();
}
Break

Default
Break
}
}
};

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Initwidgets ();
Parsexmlwithpull (check_address);
}

private void Initwidgets () {
PB = (ProgressBar) Findviewbyid (R.ID.PB);
Pb_download= (ProgressBar) Findviewbyid (r.id.pb_download);
}

Public String Getversionname () {
String versionname = "";
Packagemanager pm = Getpackagemanager ();
try {
PackageInfo PackageInfo = Pm.getpackageinfo (Package_name,
Packagemanager.get_instrumentation);
Versionname = Packageinfo.versionname;
} catch (Namenotfoundexception e) {
E.printstacktrace ();
}
return versionname;
}

public void ShowDialog () {

dialog = new Alertdialog.builder (this). Settitle ("Software update Tips")
. Setmessage ("There is a new version, do you want to update now?") ")
. Setpositivebutton ("Update Now", new Onclicklistener () {

@Override
public void OnClick (dialoginterface arg0, int arg1) {
Pb.setvisibility (View.gone);
Pb_download.setvisibility (view.visible);
New Downloadasynctask (). Execute (download_address);//Allow asynchronous tasks to download
}
}). Setnegativebutton ("Cruel refusal", new Onclicklistener () {

@Override
public void OnClick (dialoginterface arg0, int arg1) {

}
}). Create ();
Dialog.show ();
}

Parsing xml
public void Parsexmlwithpull (String address) {
Httputil.sendrequest (Address, new Httpcallbacklistener () {

@Override
public void OnFinish (InputStream is) {
try {
Xmlpullparserfactory parserfactory = xmlpullparserfactory
. newinstance ();
Xmlpullparser Pullparser = Parserfactory.newpullparser ();
Pullparser.setinput (IS, "utf-8");
int eventtype = Pullparser.geteventtype ();
APK = new apk ();

while (eventtype! = xmlpullparser.end_document) {
if (EventType = = Xmlpullparser.start_tag) {
String tagName = Pullparser.getname ();
if (tagname.equals ("name")) {
Apk.setname (Pullparser.nexttext ());
} else if (tagname.equals ("url")) {
Apk.setaddress (Pullparser.nexttext ());
} else if (Tagname.equals ("version")) {
Apk.setversionname (Pullparser.nexttext ());
}
}
EventType = Pullparser.next ();
}
Message msg = Message.obtain ();
Msg.what = 1;
Msg.obj = apk;
Handler.sendmessage (msg);
} catch (Xmlpullparserexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}

}

@Override
public void OnError (Exception e) {

}
});
}

Customize an asynchronous task to download
Class Downloadasynctask extends Asynctask<string, Integer, string> {
int currentsize = 0;
int totalsize = 0;
File file = null;

Time-consuming operation
@Override
Protected string Doinbackground (String ... params) {
try {
URL url = new URL (params[0]);
HttpURLConnection connection = (httpurlconnection) URL
. OpenConnection ();
Connection = (httpurlconnection) url.openconnection ();

Connection.setreadtimeout (5000);
Connection.setconnecttimeout (5000);
Connection.setdoinput (TRUE);
Connection.setdooutput (TRUE);
Connection.setusecaches (FALSE);
InputStream is = Connection.getinputstream ();

TotalSize = Connection.getcontentlength ();
File = new file (Environment.getexternalstoragedirectory (),
"/360weishi.apk");
DataOutputStream OS = new DataOutputStream (
New FileOutputStream (file));
int len = 0;
byte[] buffer = new byte[4096];
while (len = is.read (buffer))! =-1) {
Os.write (buffer, 0, Len);
Publishprogress (len);
}
//Where file is the downloaded files object
Intent Intent = new Intent ();
Intent.setaction ("Android.intent.action.VIEW");//Set Intent action
Intent.addcategory (" Android.intent.category.DEFAULT ");//Add extra data for Intent
Intent.setdataandtype (uri.fromfile (file),
" application/ Vnd.android.package-archive ");//Set Intent data with type
StartActivity (intent);//Activate the intent

} catch ( Malformedurlexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
Return "Download Complete";
}

Interface update
@Override
protected void Onprogressupdate (Integer ... values) {
CurrentSize + = Values[0];
Pb.setprogress ((int) (double) currentsize/totalsize * PB
. Getmax ()));
Super.onprogressupdate (values);
}

Task complete
@Override
protected void OnPostExecute (String result) {
Super.onpostexecute (result);
Toast.maketext (Getapplicationcontext (), result, Toast.length_short)
. Show ();
Pb.setvisibility (View.gone);
}

}

}

Android software version update

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.