Android SplashActivity只显示一次

问题描述:

我有一个启动屏幕活动启动主要活动,并从主要活动启动服务,当我关闭应用程序,服务仍在运行,但是当我重新启动它时,它再次显示启动画面。Android SplashActivity只显示一次

我怎样才能让它只出现一次?

可以使用上面的解决方案,或者至少检查应用程序是否第一次启动的部分。但我想你已经解决了它...

我更喜欢在我的main_activity.xml的顶部添加一个Splash Screen Layout。我通过扩展应用程序来检测应用程序的第一次启动。如果it's第一个开始,我告诉我的启动画面,而UI是建立在后台...(使用后台线程,如果进度落后!)你MainActivity

//Extend Application to save the value. You could also use getter/setter for this instead of Shared Preferences... 
public class YourApplication extends Application { 

    public static final String YOUR_APP_STARTUP = "APP_FIRST_START"; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     //set SharedPreference value to true 
     SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     SharedPreferences.Editor editor = mPreferences.edit(); 
     editor.putBoolean(YOUR_APP_STARTUP, true); 
     editor.apply();  
     ...  
    } 

检查你的第一次启动

public class YourMainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //hide actionbar and other menu which could overlay the splash screen 
    getActionBar().hide(); 

    setContentView(R.layout.activity_main); 

    Boolean firstStart = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(TVApplication.YOUR_APP_STARTUP, true); 

    if (firstStart) { 
     //First app start, show splash screen an hide it after 5000ms 
     final RelativeLayout mSplashScreen = (RelativeLayout) findViewById(R.id.splash_screen); 
     mSplashScreen.setVisibility(View.VISIBLE); 
     mSplashScreen.setAlpha(1.0f); 
     final FrameLayout mFrame = (FrameLayout) findViewById(R.id.frame_container); 
     mFrame.setAlpha(0.0f); 

     Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       Animation fadeOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), 
         R.anim.fade_out_animation); 
       fadeOutAnimation.setDuration(500); 
       fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() { 

        @Override 
        public void onAnimationStart(Animation animation) { 
         mFrame.setAlpha(1.0f); 
         getActionBar().show(); 
        } 

        @Override 
        public void onAnimationEnd(Animation animation) { 
         mSplashScreen.setVisibility(View.GONE); 
        } 

        @Override 
        public void onAnimationRepeat(Animation animation) { 

        } 
       }); 
       mSplashScreen.startAnimation(fadeOutAnimation); 
      } 
     }, 5000); //<-- time of Splash Screen shown 

    } else { 
     ((RelativeLayout) findViewById(R.id.splash_screen)).setVisibility(View.GONE); 
     getActionBar().show(); 
    } 

将SplashScreen插入main.xml中的顶部。我更喜欢RelativeLayout。在这个例子中,SplashScreen被放置在Navitgation Drawer的布局中,我们真的很喜欢,不是吗?

//main_activity.xml 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <android.support.v4.widget.DrawerLayout 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <!-- The main content view --> 

     <FrameLayout 
      android:id="@+id/frame_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

     <!-- The navigation drawer list --> 

     <ListView 
      android:id="@+id/slider_list" 
      android:layout_width="240dp" 
      android:layout_height="match_parent" 
      android:layout_alignParentTop="true" 
      android:layout_gravity="start" 
      android:background="@color/tvtv_background" 
      android:choiceMode="singleChoice" 
      android:divider="@drawable/nav_bar_divider" 
      android:dividerHeight="1dp" 
      android:listSelector="@android:color/transparent" /> 
    </android.support.v4.widget.DrawerLayout> 

    <RelativeLayout 
     android:id="@+id/splash_screen" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:background="@color/tvtv_white" 
     android:visibility="visible" > 

     <ImageView 
      android:id="@+id/splash_screen_logo" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:paddingLeft="50dp" 
      android:paddingRight="50dp" 
      android:scaleType="fitCenter" 
      android:src="@drawable/ic_launcher" /> 

     <TextView 
      android:id="@+id/splash_screen_text" 
      style="@style/TVTextBlueContent" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/splash_screen_logo" 
      android:layout_centerHorizontal="true" 
      android:padding="10dp" 
      android:text="Awesome splash shiat" /> 

     <ProgressBar 
      android:id="@+id/splash_screen_loader" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/splash_screen_text" 
      android:layout_centerHorizontal="true" 
      android:clickable="false" 
      android:indeterminate="true" /> 
    </RelativeLayout> 

</RelativeLayout> 

在您的飞溅活动中,您可以检查您的服务是否已在运行(详情请参阅How to check if a service is running on Android?)。如果服务正在运行,您可以启动主要活动并立即完成启动活动

+0

但是如果我开始意图飞溅会显示,然后只是自动去其他 – aclokay