[Android] Mobile defender welcome Page detection Update

Source: Internet
Author: User

Client: Go to the server to get the latest version information

Server-side:

Version information, latest version 2.0

Latest version of:http://xxxxxxxx/mobilesafe2.0.apk

Description information for the version

The client will enter the main interface without upgrading the new version, and the new version will replace the installation

Server-side JSON information

{"Version": "2.0", "description": " discover new version, download cash ", "Apkurl": "http://100.66.221.69/mobilesafe2.0.apk"}

Networking Request Data

Save the server's address in a configuration file,res/values/config.xml

<string name= "ServerURL" >http://xxxxxxxxxxxxxxxx</string>

Turn on child threads to check version information

New Thread{}.start (), overriding the run method

Get Url Object

New gets the Url object, catches the exception error, gets the configuration information data getString (r.string.serverurl)

Call the OpenConnection () method of the Url object to get the httpurlconnection Object

Call it in various ways to get to the data

Update Interface & Handling Exceptions

Parsing JSON

Gets the jsonobject object, constructed using the new Jsonobject (String) method

Call the get (Key) method of the jsonobject object to get the value that requires a strong turn

Define descript and Apkurl as member variables of a class

Verify if there is a new version

The current version and the version returned by the server are judged

Version consistent access to the main interface

If different, the Upgrade dialog box pops up

passing messages through Handler

define the member variable Handlerof the class in Activity , use the anonymous inner class to implement Handler, override the method Handlemessage ()

In the network access thread

Get to the Message object, call the message.obtain () method, get the existing do not new

Sets the What property of the Message Object , sets a different tag, defines a class constant

Call the sendMessage () method of the Handler object , Parameter:Message Object

Processing information

Processing in the handlemessage () method

Switch to judge the different what tags, show dialogs and Toast, jump to the main interface

Skip to Homepage

Using an explicit intent jump interface

Gets the Intent object,

Call the startactivity () method

Close the current page

Finish ()

Resolve page Jump too fast

Define a start time before networking StartTime

Networking end defines an end time endTime

If the time is less than 2 seconds,

Thread rest , minus milliseconds,thread.sleep ()

Animation of the interface jump

Gets the alphaanimation object, and constructs the parameter by new :

0.2f ,1.0f change in transparency 0.2 to 1

Call the setduration () method of the alphaanimation Object , set the time, parameter: milliseconds

Define the IDfor the interface's root layoutand find the control

Call the startanimation () method of the View object to start the animation, parameter:alphaanimation Object

 PackageCom.qingguow.mobilesafe;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL;Importorg.json.JSONException;ImportOrg.json.JSONObject;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.content.pm.PackageInfo;ImportAndroid.content.pm.PackageManager;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.Log;Importandroid.view.animation.AlphaAnimation;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportCom.qingguow.mobilesafe.utils.StreamTools; Public classSplashactivityextendsActivity {Private Static FinalString TAG = "Splashactivity"; protected Static Final intEnter_home = 0; protected Static Final intVersion_update = 1; protected Static Final intUrl_error = 2; protected Static Final intNetwork_error = 3; protected Static Final intJson_erroe = 4; PrivateTextView tv_splash_version; PrivateString description; PrivateString Apkurl; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_splash); Tv_splash_version=(TextView) Findviewbyid (r.id.tv_splash_version); Tv_splash_version.settext ("Version number" +getversionname ()); //Check for Updatescheckversion (); //Interface AnimationsAlphaanimation aa=NewAlphaanimation (0.2f, 1.0f); Aa.setduration (1000);    Findviewbyid (R.id.rl_splash_root). setanimation (AA); }    PrivateHandler Handler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {Switch(msg.what) { CaseEnter_home:enterhome ();  Break;  CaseVERSION_UPDATE:Toast.makeText (Getapplicationcontext (), Description,0). Show ();  Break;  CaseURL_ERROR:Toast.makeText (Getapplicationcontext (),"URL error", 0). Show ();                Enterhome ();  Break;  CaseNETWORK_ERROR:Toast.makeText (Getapplicationcontext (),"Network Error", 0). Show ();                Enterhome ();  Break;  CaseJSON_ERROE:Toast.makeText (Getapplicationcontext (),"JSON parsing error", 0). Show ();                Enterhome ();  Break;    }        }            }; /*** Go to homepage*/    Private voidEnterhome () {Intent Intent=NewIntent (splashactivity. This, Homeactivity.class);        StartActivity (Intent);    Finish ();    }; /*** Check the new version*/    Private voidcheckversion () {NewThread () { Public voidrun () {LongStarttime=System.currenttimemillis (); Message mes=Message.obtain ();                URL url; Try{URL=NewURL (getString (R.string.serverurl)); HttpURLConnection Conn=(httpurlconnection) URL. OpenConnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (4000); intCode =Conn.getresponsecode (); if(Code = = 200) {InputStream is=Conn.getinputstream (); String result=Streamtools.readinputstream (IS); Jsonobject JSON=Newjsonobject (Result); String newversion= (String) json.get ("version"); if(Newversion.equals (Getversionname ())) {//Enter the main interfaceMes.what =Enter_home; } Else {                            //Version UpdateMes.what =version_update; Description= (String) json.get ("description"); Apkurl= (String) json.get ("Apkurl"); }                    }                } Catch(malformedurlexception e) {e.printstacktrace (); LOG.I (TAG,"URL Error"); Mes.what=Url_error; } Catch(IOException e) {e.printstacktrace (); LOG.I (TAG,"Network connection Error"); Mes.what=Network_error; } Catch(jsonexception e) {e.printstacktrace (); LOG.I (TAG,"JSON parsing error"); Mes.what=Json_erroe; } finally {                    //Delay Effect                    LongEndtime=System.currenttimemillis (); Longdtime=endtime-StartTime; if(dtime<3000){                        Try{Thread.Sleep (3000-dTime); } Catch(Interruptedexception e) {}} handler.sendmessage (MES);        }            };    }.start (); }    //get the app version name    PrivateString Getversionname () {Packagemanager pm=Getpackagemanager (); Try{PackageInfo Info= Pm.getpackageinfo (Getpackagename (), 0); returnInfo.versionname; } Catch(Exception e) {e.printstacktrace (); return""; }    }}

[Android] Mobile defender welcome Page detection 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.