Mobile Security Defender's study (1)

Source: Internet
Author: User

The main study today is the initialization of the Splashactivity page, which involves a lot of knowledge points

1 Get package Name get version number information

2 Checking for upgrades and parsing of JSON data using third-party tools

3 Thread and message usage

 Packagecom.djf.mobilesafty;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL;Importorg.json.JSONException;ImportOrg.json.JSONObject;ImportCom.djf.mobilesafty.utils.StreamTools;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.content.pm.PackageInfo;ImportAndroid.content.pm.PackageManager;Importandroid.content.pm.PackageManager.NameNotFoundException;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;Importandroid.text.Annotation;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;Importandroid.view.animation.AlphaAnimation;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classSplashactivityextendsActivity {protected Static FinalString TAG = "Splashactivity"; protected Static Final intEnter_home = 0; protected Static Final intShow_dialog = 1; protected Static Final intUrl_error = 2; protected Static Final intNet_error = 3; protected Static Final intJson_error = 4; PrivateTextView Tv_splash_verison; PrivateString description; PrivateString Apkurl; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Tv_splash_verison=(TextView) Findviewbyid (R.id.tv_splash_verison); Tv_splash_verison.settext ("Version number:" +getversionname ()); Alphaanimation AA=NewAlphaanimation (0.2f, 1.0f); Aa.setduration (500);        Findviewbyid (R.layout.activity_main). startanimation (AA); //Check for upgradescheckupdate (); }    PrivateHandler Handler =NewHandler () { Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); Switch(msg.what) { CaseEnter_home://go to the main pageEnterhome ();  Break;  CaseShow_dialog://Show dialog BoxLOG.I (TAG, "Show upgrade dialog box");  Break;  CaseUrl_error://URL ErrorEnterhome (); Toast.maketext (Getapplicationcontext (),"URL error", 0). Show ();  Break;  CaseNet_error://Network ErrorEnterhome (); Toast.maketext (splashactivity. This, "network Error", 0). Show ();  Break;  CaseJson_error://JSON parsing errorEnterhome (); Toast.maketext (splashactivity. This, "JSON parsing error", 0). Show ();  Break; default:                 Break;    }        }    }; protected voidEnterhome () {//TODO auto-generated Method StubIntent Intent =NewIntent ( This, Homeactivity.class);        StartActivity (Intent); //Close the current pagefinish ();    }; /*** Check if upgrade is required*/    Private voidcheckupdate () {// http://125.39.51.195: 8080/updatinfo.html        NewThread () { Public voidrun () {Message msg=Message.obtain (); LongStartTime =System.currenttimemillis (); Try{URL URL=NewURL (getString (R.string.serverurl)); HttpURLConnection Connection=(httpurlconnection) URL. OpenConnection (); Connection.setrequestmethod ("GET"); Connection.setreadtimeout (4000); intCode =Connection.getresponsecode (); if(Code = = 200) {InputStream in=Connection.getinputstream (); //turn a stream into a stringString result =Streamtools.readfromstream (in); LOG.I (TAG,"Networking Success" +result); //JSON parsingJsonobject obj =Newjsonobject (Result); //get version information for the serverString version = (string) obj.get ("version"); Description= (String) obj.get ("description"); Apkurl= (String) obj.get ("Apkurl"); //Verify if there is a new version                        if(Getversionname (). Equals (version)) {//version ConsistentMsg.what =Enter_home; } Else {                            //inconsistent versionMsg.what =Show_dialog; }                    }                } Catch(malformedurlexception e) {msg.what=Url_error;                E.printstacktrace (); } Catch(IOException e) {msg.what=Net_error;                E.printstacktrace (); } Catch(jsonexception e) {//TODO auto-generated Catch blockMsg.what =Json_error;                E.printstacktrace (); } finally {                    LongEndtime =System.currenttimemillis (); LongDtime = Endtime-StartTime; if(Dtime < 2000) {                        Try{Thread.Sleep (2000-dtime); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace ();                }} handler.sendmessage (msg);        }            };    }.start (); }    /*** Get version of application information*/    PrivateString Getversionname () {Packagemanager Pmanager=Getpackagemanager (); Try{PackageInfo Info= Pmanager.getpackageinfo (Getpackagename (), 0); returnInfo.versionname; } Catch(namenotfoundexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        return""; }}

Mobile Security Defender's study (1)

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.