安卓初始屏幕

My Package Explorer

这就是我在包资源管理器中所拥有的,所以让我们从顶部开始,并解决我认为它所在的问题。

主要活性.java -

 package com.drg.idoser;

 import android.os.Bundle;
 import android.app.Activity;
 import android.view.Menu;

 public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

现在飞溅活动.java

package com.drg.idoser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class SplashActivity extends Activity {

 private static String TAG = SplashActivity.class.getName();
 private static long SLEEP_TIME = 5;    // Sleep for some time

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  this.requestWindowFeature(Window.FEATURE_NO_TITLE);    // Removes title bar
  this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,     WindowManager.LayoutParams.FLAG_FULLSCREEN);    // Removes notification bar

  setContentView(R.layout.splash);

  // Start timer and launch main activity
  IntentLauncher launcher = new IntentLauncher();
  launcher.start();
}

 private class IntentLauncher extends Thread {
  @Override
  /**
   * Sleep for some time and than start new activity.
   */
  public void run() {
     try {
        // Sleeping
        Thread.sleep(SLEEP_TIME*1000);
     } catch (Exception e) {
        Log.e(TAG, e.getMessage());
     }

     // Start main activity
     Intent intent = new Intent(SplashActivity.this, MainActivity.class);
     SplashActivity.this.startActivity(intent);
     SplashActivity.this.finish();
  }
}
}

activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

飞溅.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@drawable/splash_bg"
android:layout_height="match_parent"
android:orientation="vertical" >


</LinearLayout>

AndroidManafest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.drg.idoser"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.drg.idoser.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>
</application>

</manifest>

现在我认为我的问题是在AndroidManafest中.xml我不认为我在AndroidManafest中设置了splach屏幕.xml当我从手机启动我的应用程序时,它会跳转到activity_main.xml而不是飞溅.xml我是Android应用程序的新手,所以我似乎找不到我的问题,但我需要我的闪屏显示5秒,如果有人有TeamViwer并希望帮助我发布我的会话信息,如果它会更快。


答案 1

将您的代码更改为以下内容。您没有声明 SplashActivity,并且已将 MainActivity 设置为启动器活动。<application>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.drg.idoser.SplashActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.drg.idoser.MainActivity"
        android:label="@string/app_name" />
</application>

答案 2

实现初始屏幕的最佳方式是创建一个新活动,每次启动应用程序时都会显示。

public class SplashScreen extends Activity {

private Handler mHandler;

private Runnable myRunnable;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Just create simple XML layout with i.e a single ImageView or a custom layout
    setContentView(R.layout.splash_screen_layout);
    mHandler = new Handler();
    myRunnable = new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    };

}

@Override
public void onBackPressed() {
// Remove callback on back press
    if (mHandler != null && myRunnable != null) {
        mHandler.removeCallbacks(myRunnable);
    }
    super.onBackPressed();
}

@Override
protected void onPause() {
// Remove callback on pause
    if (mHandler != null && myRunnable != null) {
        mHandler.removeCallbacks(myRunnable);
    }
    super.onPause();
}

@Override
protected void onResume() {
// Attach and start callback with delay on resume
    if (mHandler != null && myRunnable != null) {
        mHandler.postDelayed(myRunnable, ConstantValues.SPLASH_TIME_OUT);
    }
    super.onResume();
}
}

推荐