Android instance-Mobile security Defender (i)-Launch interface

Source: Internet
Author: User

First, the goal.

1, Start full screen display, and get the current application version number;

2, beautify the font: color and text shadow;

3. Add a ProgressBar to increase the user experience.

Effect

Second, the preparation process.

1, layout. Add a TextView and a ProgressBar to the layout file and arrange it as required (this example uses relativelayout for placement).

①. The Android:background property in Relativelayout finds the layout file under the Drawable file by @id method.

②. In TextView by android:shadowcolor= "RGB Color" (set shadow color), android:shadowdx= "int" (set shadow X-axis offset), android:shadowdy= "int" ( Sets the shadow y-axis offset), android:shadowradius= "int" (Sets the shadow radius). Set the font color to white, the default content is "version 1.0", which is located in the middle of relativelayout.

③. Place a ProgressBar below the TextView.

The layout code is as follows:

1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:background= "@drawable/LAUNCHER_BG"6Tools:context= ". Splashactivity ">7 8<TextView9Android:id= "@+id/tv_version"TenAndroid:layout_width= "Wrap_content" Oneandroid:layout_height= "Wrap_content" AAndroid:layout_centerinparent= "true" -Android:shadowcolor= "#00ffff" -android:shadowdx= "1" theandroid:shadowdy= "1" -android:shadowradius= "1" -android:text= "Version 1.0" -Android:textcolor= "#ffffff" +Android:textsize= "22SP"/> -  +<ProgressBar AAndroid:layout_width= "Wrap_content" atandroid:layout_height= "Wrap_content" -android:layout_below= "@id/tv_version" -Android:layout_centerhorizontal= "true"/> -  -</RelativeLayout>
View Code

2. Start style. Full screen when the program starts.

In the program configuration file (Androidmanifest.xml), the Android:theme attribute is added to the Activity node under the application node, whose value = "@android: style/ Theme.NoTitleBar.Fullscreen ". There are many styles to choose from, theme.

The style code is as follows:

1 <activity2             android:name= "com.example.mobilesafe.SplashActivity"3             android:label= "@string/app_name"4             android:theme= "@android: Style/theme.notitlebar.fullscreen" >
View Code

3, the code implementation version number display.

①. Create a new private method in the main code that returns a value type string, named GetVersionNumber ();

②. In the GetVersionNumber () method, the return value of the Packagemanager type is obtained by the Getpackagemanager () method, named PM;

③. The Getpackageinfo (PackageName, Flags) method of the Packagemanager type Object (PM) Gets the return value of the PackageInfo type, named PackageInfo. The parameter packagename in the Getpackageinfo (packagename, Flags) method refers to the full name of the package for which you want to obtain package information. For example (com.google.apps.contacts), if you want to obtain the full name of the package corresponding to this program, you can use the Getpackagename () method;flags is a label of type int and is currently available in 0 instead. At the same time, this method will produce an exception, using try{...} Catch{...} Module capture.

④. The return value of type string can be obtained by the Versionname method of the PackageInfo type Object (PackageInfo), which is the version number of the PackageName. is actually the value in the Android:versionname under the Manifest node in the program configuration file (Androidmanifest.xml).

⑤. Finally, change the text content in the TextView by the SetText ("text") method.

The implementation code is as follows:

1  PackageCom.example.mobilesafe;2 3 ImportAndroid.os.Bundle;4 Importandroid.app.Activity;5 ImportAndroid.content.pm.PackageInfo;6 ImportAndroid.content.pm.PackageManager;7 Importandroid.content.pm.PackageManager.NameNotFoundException;8 ImportAndroid.view.Menu;9 ImportAndroid.widget.TextView;Ten  One  Public classSplashactivityextendsActivity { A     PrivateTextView tv_version; -  - @Override the     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_splash); -Tv_version =(TextView) Findviewbyid (r.id.tv_version); +Tv_version.settext ("version" +GetVersionNumber ()); -     } +      A     /* at * Get Application Version number -      */ -      -     PrivateString GetVersionNumber () { -         //apk for managing your phone -Packagemanager pm =Getpackagemanager (); in         //get the feature manifest file for the specified apk (that is, androidmainfest.xml) -         Try { toPackageInfo PackageInfo = Pm.getpackageinfo (Getpackagename (), 0); +             returnPackageinfo.versionname; -}Catch(namenotfoundexception e) { the e.printstacktrace (); *             return""; $         }                Panax Notoginseng     } -}
View Code

Third, check.

You can change the version number at startup by changing the value in the Android:versionname under the Manifest node in the program configuration file (Androidmanifest.xml).

Android Instance-Mobile security Defender (i)-Start interface

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.