[英]start Splash screen activity
I want to write a code that uses the splash screen .I have written this so far, but Can anyone tell me what is the missing here!? 我想编写一个使用启动画面的代码。到目前为止,我已经写了这篇文章,但是谁能告诉我这里缺少什么!
here is my main code: 这是我的主要代码:
package com.example.splash;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
and here is my splash activity code: 这是我的启动活动代码:
package com.example.splash;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class splashscreen extends Activity {
protected int _splashTime = 5000;
private Thread splashTread;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splashh);
final splashscreen sPlashScreen = this;
splashTread = new Thread() {
@Override
public void run() {
try {
synchronized(this){
wait(_splashTime);
}
} catch(InterruptedException e) {}
finally {
finish();
Intent i = new Intent();
i.setClass(sPlashScreen,MainActivity.class);
startActivity(i);
//stop();
}
}
};
splashTread.start();
}
The problem is I do not know how to tell my main to go splash activity , if I use an intent I would stuck on infinite loop. 问题是我不知道如何告诉我的主打飞溅活动,如果使用意图,我将陷入无限循环。
You can simply use this: 您可以简单地使用:
Handler handler=new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
Intent intent = new Intent(SplashViewController.this,HomeViewController.class);
startActivity(intent);
SplashViewController.this.finish();
}
}, 3000);
try this instead : 试试这个代替:
public class splashscreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
Thread t = new Thread(Splash_Runnable);
t.start();
}
Runnable Splash_Runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
startActivity(new Intent(splashscreen.this,
MainActivity.class));
splashscreen.this.finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
The problem (i guess) is that your app is starting with your MainActivity
as your launcher Activity. 问题(我想)是您的应用程序以MainActivity
作为启动器Activity开始。 Make splashscreen
your laucher Activity in your Application Manifest XML and you will avoid the infinite loop. 在您的Application Manifest XML中将splashscreen
您的laucher Activity,您将避免无限循环。
Try this code: 试试这个代码:
private boolean _active = true;
private int _splashTime = 5000;
Thread splashTread = new Thread()
{
@Override
public void run()
{
try
{
int waited = 0;
while(_active && (waited < _splashTime))
{
sleep(100);
if(_active)
{
waited += 100;
}
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(SplashScreenActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
};
splashTread.start();
in AndroidManifest
mention your activity as Main Activity. 在AndroidManifest
将您的活动称为“主要活动”。
Try to change your SplashActivity code from here. 尝试从此处更改您的SplashActivity代码。
Splash and main activity error 飞溅和主要活动错误
Also make your splashactivtiy as your launcher activity and then redirect to the MainActivity from the SplashScreen 还可以将启动活动作为启动活动,然后从SplashScreen重定向到MainActivity
<activity
android:name="com.app.wablogic.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
Full detail of creating a splash Activity 创建启动活动的完整细节
Create a layout for Splash 为飞溅创建布局
splash.xml 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:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/splash" >
</LinearLayout>
Now create a class Under package . 现在创建一个类Under package。 Name it Splash 命名为Splash
public class Splash extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
startActivity(openMainActivity);
finish();
}
}, 5000); //it will call the MainActivity after 5 seconds
}
Go to manifest
and add the Activity
to it. 转到manifest
并将Activity
添加到其中。
and cut the intent-filter
where main and Launcher are child and paste it in Splash Activity like 并在main和Launcher是子级的地方剪切intent-filter
,然后将其粘贴到Splash Activity中,例如
<activity
android:name="com.example.yourpackage.Splash"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can create a Thread for doing something or just sleep for a few seconds to do, such as 您可以创建一个线程来做某事,或者只是睡几秒钟来做,例如
public class MainActivity extends Activity { 公共类MainActivity扩展了Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 3 seconds
sleep(3*1000);
// After 3 seconds redirect to another intent
Intent i=new Intent(getBaseContext(),MenuActivity.class);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
background.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.