Explain the programming method of the startup interface Splash of Android app _android

Source: Internet
Author: User
Tags stub

The function of Splash interface
logo used to show the product
Application initialization actions
Check the version of the application
Check if the current application is legally registered

Second, the interface of the XML definition
write a layout background set to the product logo picture, and then add a TextView display version number.

  <textview
  android:id= "@+id/tv_splash_version"
  android:layout_width= "Wrap_content"
  android: layout_height= "Wrap_content"
  android:textcolor= "#000000"
  android:textstyle= "bold"
  ANDROID:SHADOWDX = "1"//Shadow offset
  android:shadowdy= "1"
  android:shadowradius= "0.2"//Shadow Radius
  android:shadowcolor= "#ffff00 "
  android:text=" version: 1.0 "
  android:textsize=" 16sp "
  android:layout_centerinparent=" true "/>

Third, the method of dynamically acquiring version number

Public String getappversion () {
  Packagemanager pm = Getpackagemanager ();
  try {
    PackageInfo info = pm.getpackageinfo (getpackagename (), 0);
    return info.versionname;
  } catch (Namenotfoundexception e) {
    e.printstacktrace ();
    impossible to happen; return
    "";
  }
}

Four, linked server get update information
dialog box for upgrade reminders

protected void Showupdatedialog () {
 Alertdialog.builder build = new Builder (this);
 Build.settitle ("Discovery of New Version");
 Build.setmessage (description);
 Build.setnegativebutton ("Upgrade Now", New Onclicklistener () {

   @Override public
   void OnClick (dialoginterface dialog, int which) {
     //upgraded code;
   };};
 Build.setpositivebutton ("Next time Again", New Onclicklistener () {

   @Override public
   void OnClick (dialoginterface dialog, int which) {
     Dialog.dismiss ();
 Enterhome ();

   }
 );
 Build.show ();

Request a server's code checkup () method in a child thread

private void Checkup () {new Thread () {public void run () {msg = Message.obtain (); Long starttime = System.currenttimemillis ()//The system time at which the thread is started try {///request the Code URL URL of the network = new URL (getString R.
       String.serverurl));
       HttpURLConnection conn = (httpurlconnection) URL. OpenConnection (); Conn.setrequestmethod ("get");//Request Method Conn.setconnecttimeout (4000);/Timeout int code = Conn.getresponsecode ();//
         Return code 200 to request success if (code = =) {InputStream is = Conn.getinputstream ();
         String result = Streamtools.readfromstream (IS);
         LOG.I (TAG, "networking success" + result);
         Jsonobject obj = new Jsonobject (result);//parse JSON string version = (string) obj.get ("version");//versioning information
         Description = (string) obj.get ("description");//Description Information Apkurl = (string) obj.get ("Apkurl");
         if (Getappversion (). Equals (version)) {msg.what = Enter_home; else {msg.what = SHOW_update_dialog;
       A catch (Malformedurlexception e) {e.printstacktrace ());
     Msg.what = Url_error;
       catch (IOException e) {e.printstacktrace ();
     Msg.what = Network_error;
       catch (Jsonexception e) {e.printstacktrace ();
     Msg.what = Json_error;
       finally {handler.sendmessage (msg); Long endtime = System.currenttimemillis ()//The time the thread has finished executing long dTime = endtime-starttime;//The blocked time of the thread if (DTIME&L  t;3000) {try {thread.sleep (3000-dtime);//If the thread has a blocking time of less than three seconds to continue sleep to three seconds} catch (interruptedexception
         e) {e.printstacktrace ();

 }}}}.start (); Handler Private Handler handler = new Handler () {@Override public void Handlemessage (msg) {//TODO Aut
   o-generated method Stub super.handlemessage (msg);
     Switch (msg.what) {case Show_update_dialog:showupdatedialog ();
   Break Case Enter_home: Toast.maketext (Getapplicationcontext (), "", 0). Show ();
     Enterhome ();
   Break
     Case URL_ERROR:Toast.makeText (Getapplicationcontext (), "Url_error", 0). Show ();
     Enterhome ();
   Break
     Case NETWORK_ERROR:Toast.makeText (Getapplicationcontext (), "Network_error", 0). Show ();
     Enterhome ();

   Break
     Case JSON_ERROR:Toast.makeText (Getapplicationcontext (), "Json_error", 0). Show ();
     Enterhome ();
   Break

 }
 }
};

Five, download the file (using the Afinal framework) and invoke the System Setup tool to install APK

  if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {finalhttp finalhttp = new FinalHtt
        P ();  Finalhttp.download (Apkurl,environment.getexternalstoragedirectory (). GetAbsolutePath () + "/mobilesafe2.0.apk", new
            Ajaxcallback<file> () {@Override public void onloading (long count, long current) {
            Super.onloading (count, current);
            Tv_uapdate_info.setvisibility (view.visible);
                int progress = (int) (current*100/count);
          Tv_uapdate_info.settext ("Download Progress:" +progress+ "%");
            @Override public void OnFailure (Throwable t, int errorno, String strMsg) {
            T.printstacktrace ();
            Toast.maketext (Getapplicationcontext (), "Download Failed", 0). Show ();

            Enterhome ();
          Super.onfailure (t, Errorno, STRMSG);
     @Override public void onsuccess (File t) {super.onsuccess (t);       INSTALLAPK (t); The private void installapk (File t) {Intent Intent = new Intent ()//The automatic installer can call the code Intent
            . Setaction ("Android.intent.action.VIEW");
            Intent.addcategory ("Android.intent.category.DEFAULT");
            Intent.setdataandtype (Uri.fromfile (t), "application/vnd.android.package-archive");            
          StartActivity (Intent);

      }

        });
        }else{Toast.maketext (Getapplicationcontext (), "Please insert a memory card and try again", 0). Show ();

      Return

 }

Other:

1, Show 4.0 style: The way is to remove the function of the list of activity corresponding to the android:theme;
Put it inside the application;

2, when the splash page pop-up upgrade prompts box filter Click Return is two ways:

Builder.setcancelable (FALSE);
Set Setoncancellistener to go directly to the main page when touching the screen

The dialog box is mounted on the activity, and if the activity does not exist, the dialog box cannot be created.
Getapplicationcontext (); The life cycle is long, it exists as long as the application survives; this has a short lifecycle, and as long as the activity does not exist, the system will recycle
Wherein: Getbasecontext (), Getapplication (), Getapplicationcontext (); Can not be placed in the context of alertdialog;

3.Splash is useful for promoting and hiding program startup details.

The implementation method with handler is as follows: (can also be implemented by thread, not recommended)
Define an activity to display your picture, the most important of which is to define a handler that is used to send and receive messages:

public class Welcomeactivity extends activity  
{ 
  //define a Handler to receive deferred messages-start activity 
  private Handler Handler = new Handler () 
  { 
    @Override 
    <span style= "color: #ff0000;" >public void Handlemessage (msg) </span> 
    { 
      //TODO auto-generated method stub 
      Super.handlemessage (msg); 
      Switch (msg.what) 
      {case 
      0x123: 
        Intent Intent = new Intent (Welcomeactivity.this,  
            Onlineexamactivity.class); 
        StartActivity (intent); 
        Finish ();}} 
  ; 


In the OnCreate () method, send a message with handler for a delay of 3000 milliseconds:

@Override 
  protected void onCreate (Bundle savedinstancestate) 
  { 
    //TODO auto-generated method stub 
    Super.oncreate (savedinstancestate); 
    Setcontentview (r.layout.welcome_activity); 
    Start (); 
  } 
 
  private void Start ()  
  { 
    <span style= "color: #ff0000;" >handler.sendemptymessagedelayed (0x123, 3000);</span> 
  } 

Put your picture in the layout file as a background.

Related Article

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.