安卓启动屏幕

14

我的包资源管理器

这是我在包资源管理器中拥有的内容,让我们从顶部开始,逐步解决问题,找到我认为存在的位置...

MainActivity.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;
}

}

现在是 SplashActivity.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>

splash.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.xml文件中设置不正确。当我从手机上启动应用程序时,它跳转到activity_main.xml而不是splash.xml。我是android应用程序的新手,所以似乎找不到我的问题,但我需要我的闪屏界面显示5秒钟。如果有人有TeamViwer并想帮助我,我将发布我的会话信息,如果这样会更快的话。

1
对于那些可能会复制上面闪屏代码的人,强烈建议不要使用Thread。相反,请使用Handler。请查看链接http://stackoverflow.com/a/12195205/361100。 - Youngjae
请查看此链接 https://github.com/meetmehdi/GoodSplash。 - Syed Raza Mehdi
5个回答

25

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

<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>

我尝试了,它说请等待25分钟,我不知道为什么。 - Jacob Anthony Tonna
对我来说完美地工作了。谢谢。 - Zapnologica

2

最佳实现应用启动画面的方式是创建一个新的活动(activity),每次应用程序启动时都会显示该画面。

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();
}
}

2
这是一个简单的例子!

~Lunox

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

splashscreen.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class splashscreen extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);

        //Splash Screen duration
        int secondsDelayed = 1;
        new Handler().postDelayed(new Runnable() {
            public void run() {
                startActivity(new Intent(splashscreen.this, MainActivity.class));
                finish();
            }
        }, secondsDelayed * 3000);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

splashscreen.xml

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

    />

splashlogo.png

splashlogo.png

GitHub

启动画面


完成了兄弟!感谢您的反馈! - Lunox

1
你可以像这样轻松访问启动画面。例如:
public class MainActivity extends Activity {
    private ImageView splashImageView;
    private boolean splashloading = false;

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

        splashImageView = new ImageView(this);
        splashImageView.setScaleType(ScaleType.FIT_XY);
        splashImageView.setImageResource(R.drawable.ic_launcher);
        setContentView(splashImageView);
        splashloading = true;
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            public void run() {
                splashloading = false;
                // set your layout file in below
                setContentView(R.layout.activity_main);
            }
        }, 3000);

    }
}

它将会100%地工作。


-1
@Override

   protected void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

          setContentView(R.layout.activity_main);

          Thread th = new Thread(new Runnable() {            /*create a new thread */

                              @Override

                              public void run() { /*

                                                                  * The purpose of this thread is to

                                                                  * navigate from one class to another

                                                                  * after some time

                                                                  */

                                     try {

                                            Thread.sleep(5000);

                                     } catch (InterruptedException e) {

                                            /*

                                             * We are creating this new thread because we don’t

                                             * want our main thread to stop working for that

                                             * time as our android stop working and some time

                                             * application will crashes

                                             */

                                            e.printStackTrace();

                                     }

                                     finally {

                                            Intent i = new Intent(MainActivity.this,

                                                          Splash_Class.class);

                                            startActivity(i);

                                            finish();

                                     }

                              }

                       });

          th.start(); // start the thread

   }

http://www.codehubb.com/android_splash_screen


请修正您的缩进! - Lee Taylor
3
需要解释一下会更好,仅有的代码并不太有用。 - John Dvorak
欢迎来到Stack Overflow!如果目标网站无法访问或永久离线,请引用链接中最相关的部分。请参阅如何撰写一个好答案 - Maveňツ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接