Two Methods for android startup screen: android

Source: Internet
Author: User

Two Methods for android startup screen: android

Now, when we open any app, most of them will display a startup interface, displaying the company's logo and current version, and some will directly put the advertisement on it. The startup screen can be set in two ways:One is the implementation of two activities.,And a Ativity implementation. The following describes two ways to set the startup screen:

I. Two activitiesSource code:

Import android. app. activity; import android. content. intent; import android. OS. bundle; import android. OS. handler; import android. view. window; public class SplashActivity extends Activity {private static int SPLASH_DISPLAY_LENGHT = 6000; // latency of 6 seconds @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); getWindow (). requestFeature (Window. FEATURE_NO_TITLE); // remove the title setContentView (R. layout. activity_splash); new Handler (). postDelayed (new Runnable () {public void run () {Intent intent = new Intent (SplashActivity. this, MainActivity. class); startActivity (intent); SplashActivity. this. finish (); // close the splashActivity and recycle it. Otherwise, press the return key to return to this interface. }}, SPLASH_DISPLAY_LENGHT );}}

 

Don't forget to set AndroidManifest. xml

        <activity             android:name="com.example.andorid_splash_0.SplashActivity"            android:label="splash">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>                    </activity>        <activity            android:name=".MainActivity"            android:label="@string/app_name" >        </activity>

 

It is easy to see that SplashActivity is started before MainActivity. It takes 6 seconds to start MainActivity.

Additional knowledge:

// Immediately execute the Runnable object public final boolean post (Runnable r); // execute the Runnable object public final boolean postAtTime (Runnable r, long uptimeMillis) at the specified time (uptimeMillis ); // execute the Runnable object public final boolean postDelayed (Runnable r, long delayMillis) at the specified interval (delayMillis );

 

2. Start an Activity

First look at the layout file: There are two screen-filled ImageView and TextView

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <LinearLayout android: id = "@ + id/splashScreen" android: layout_width = "match_parent" android: layout_height = "match_parent"> <ImageView android: id = "@ + id/iv_image" android: layout_width = "match_parent" android: layout_height = "match_parent" android: src = "@ drawable/new00"/> </LinearLayout> <TextView android: layout_width = "match_parent" android: layout_height = "match_parent" android: textSize = "100dp" android: gravity = "center" android: text = "Main Interface"/> </LinearLayout>

 

Activity Code:

Import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. OS. systemClock; import android. view. view; import android. widget. imageView; import android. widget. linearLayout; public class MainActivity extends Activity {private LinearLayout splash; private ImageView iv_image; private static final int STOPSPLASH = 0; private static final long SPLASHTIME = 1000; private Handler splashHandler = new Handler () {public void handleMessage (Message msg) {switch (msg. what) {case STOPSPLASH: SystemClock. sleep (4000); // sleep 4 s splash. setVisibility (View. GONE); break;} super. handleMessage (msg) ;}}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); splash = (LinearLayout) findViewById (R. id. splashScreen); Message msg = new Message (); msg. what = STOPSPLASH; splashHandler. sendMessageDelayed (msg, SPLASHTIME); // After the SPLASHTIME, send the message }}

 

Iii. Summary:

The above two methods can achieve the boot screen before the application starts, but in actual development, we recommend that you use the first one better, because the main interface code should not be too much, should be concise.

 

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.