2016-11-18 163 views
0

我嘗試在應用程序啓動時放置圖像。我有MainActivity,使第二acivity這樣的:應用程序啓動時沒有啓動活動

public class SplashActivity extends Activity 
{ 
    private static final long DELAY = 3000; 
    private boolean scheduled = false; 
    private Timer splashTimer; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 

     splashTimer = new Timer(); 
     splashTimer.schedule(new TimerTask() 
     { 
      @Override 
      public void run() 
      { 
       SplashActivity.this.finish(); 
       startActivity(new Intent(SplashActivity.this, MainActivity.class)); 
      } 
     }, DELAY); 
     scheduled = true; 
    } 

    @Override 
    protected void onDestroy() 
    { 
     super.onDestroy(); 
     if (scheduled) 
      splashTimer.cancel(); 
     splashTimer.purge(); 
    } 
} 

SplashAcitivity.xml我補充一下:

android:background="@drawable/my_image" 

和OFC我複製my_image到文件夾繪製

Android的工作室沒有下劃線在我的代碼,似乎好,但是當我運行這個應用程序時,應用程序像以前一樣工作,只用MainActivity。沒有想法從我如何解決它。

+0

SplashActivity.this.finish();在startActivity之後調用此方法,並將splashactivity設置爲在啓動程序中顯示或不顯示? – Bali

回答

0

您編輯AndroidManifedst以在MainActivity之前運行splashScreen?

你需要進行改變,你有

<activity 
    android:name=".MainActivity" 
    android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

您需要更改

<category android:name="android.intent.category.LAUNCHER" /> 

<category android:name="android.intent.category.DEFAULT" /> 

,並在閃屏把

<category android:name="android.intent.category.LAUNCHER" /> 
+0

只有當我把它放在SplashScreen中'這是正常的嗎? – patrick1980

+0

是的,您需要複製活動代碼,但將MainActivity更改爲Default並將SplashScreen設置爲啓動器 – BrunoM24

相關問題