The App Guide page is an essential element for every premium app, so let's look at how to load the boot page only the first time you enter the app.
1. Determine whether to enter the application for the first time:
Package com.yayun.guide; Import Android.app.activity;import Android.content.context;import Android.content.intent;import Android.content.sharedpreferences;import Android.os.bundle;import Android.os.handler;import android.os.Message; Import Android.widget.Toast; /** * Function: Use Viewpager to achieve the first time to enter the application of the boot page * * (1) Determine whether it is the first time to load the application-take the method of reading the sharedpreferences * (2) is, then enter the boot activity; No, enter the Mainactivity * (3) 5s after execution (2) operation * * @author Yayun * */public class Splashactivity extends Activity {@Override protected void oncr Eate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_splash); Boolean mfirst = Isfirstenter (Splashactivity.this,splashactivity.this.getclass (). GetName ()); Toast.maketext (This, mfirst+ "", Toast.length_short). Show (); if (Mfirst) mhandler.sendemptymessagedelayed (switch_guidactivity,100); else mhandler.sendemptymessagedelayed (switch_mainactivity,100); Sharedpreferences sharedpreferences= this.getsharedpreferences ("My_pref", mode_private); Sharedpreferences.edit (). putstring ("Guide_activity", "false"). commit (); }//****************************************************************//Determine if the application was first loaded, read the GUID in the Sharedpreferences e_activity field//**************************************************************** private static final String SHAREDPR Eferences_name = "My_pref"; private static final String key_guide_activity = "guide_activity"; Private Boolean Isfirstenter (Context context,string className) {if (Context==null | | classname==null| | "". Equalsignorecase (ClassName)) return false; String mresultstr = context.getsharedpreferences (Sharedpreferences_name, context.mode_world_readable). Get String (Key_guide_activity, "");//Get all class names such as Com.my.MainActivity if (Mresultstr.equalsignorecase ("false")) R Eturn false; else return true; } //************Handler: Jump to different page//************************************************* Priv ATE final static int switch_mainactivity = 1000; Private final static int switch_guidactivity = 1001; Public Handler Mhandler = new Handler () {public void Handlemessage (Message msg) {switch (msg.what) { Case Switch_mainactivity:intent mintent = new Intent (); Mintent.setclass (Splashactivity.this, Welcome.class); SplashActivity.this.startActivity (mintent); SplashActivity.this.finish (); Break Case switch_guidactivity:mintent = new Intent (); Mintent.setclass (Splashactivity.this, Guideactivity.class); SplashActivity.this.startActivity (mintent); SplashActivity.this.finish (); Break } super.handlemessage (msg); } };}
The principle is very simple, enter the time to judge whether there is no value, if no value is the first time to enter, then jump into the Application Guide page, otherwise, jump into the main page.
2.ViewPager to achieve page turn effect:
Package Com.yayun.guide;import Java.util.arraylist;import Java.util.list;import android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.support.v4.view.pageradapter;import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.view.window;import Android.widget.button;import Android.widget.LinearLayout ;p ublic class Guideactivity extends Activity implements Onclicklistener{private Viewpager mviewpager;private Pageradapter madapter;private list<view> mviews = new arraylist<view> ();//Tabprivate LinearLayout Mtabweixin;private linearlayout mtabfrd;private linearlayout mtabaddress;private LinearLayout mTabSetting;private Button Menterbutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate) ; Requestwindowfeature (Window. Feature_no_title); Setcontentview (R.layout.activity_main); Initview (); initevents ();} private void Initevents () {Mviewpager.setonpagechangelistener (new Onpagechangelistener () {@Overridepublic void onpageselected (int arg0) {} @Overridepublic void onpagescrolled (int arg0, float arg1, int arg2) {} @Overridepublic void onpagescrollstatechanged (int arg0) {}});} private void Initview () {Mviewpager = (Viewpager) Findviewbyid (R.id.id_viewpager); Layoutinflater Minflater = Layoutinflater.from (this); View tab01 = minflater.inflate (R.LAYOUT.TAB01, NULL); View tab02 = minflater.inflate (R.LAYOUT.TAB02, NULL); View tab03 = minflater.inflate (r.layout.tab03, NULL); View tab04 = minflater.inflate (r.layout.tab04, null); Mviews.add (TAB01); Mviews.add (TAB02); Mviews.add (TAB03); Mviews.add (TAB04); menterbutton= (Button) Tab04.findviewbyid (r.id.imgbtn_enter); Menterbutton.setonclicklistener ( New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent intent=new Intent (guideactivity.this,welcome.class ); StartActivity (intent);}}); madapter = new Pageradapter () {@Overridepublic void Destroyitem (ViewGroup container, int position,object Object) { Container.removeview (Mviews.get (position));} @Overridepublic Object Instantiateitem (viewgroup container, int position) {View view = Mviews.get (position); Container.addview (view); return view;} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;} @Overridepublic int GetCount () {return mviews.size ();}}; Mviewpager.setadapter (Madapter);} @Overridepublic void OnClick (View v) {}}
3. Boot page layout file:
<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" > <android.support.v4.view.viewpager android:id= "@+id/id_ Viewpager " android:layout_width=" match_parent " android:layout_height=" match_parent " > </ Android.support.v4.view.viewpager></linearlayout>
Run the code below:
We can find that the first time you enter the page, the toast prints true, indicating that the application first enters, then loads the boot page.
Quit the app, and once again, toast prints false, indicating that the app is not the first time it's entered. At this point our functions are basically realized.
Favorite friends on the left sidebar follow me!
SOURCE download
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Real-time Easy tutorial-64th gun (Android App boot page implementation-load on first app entry)