[英]Splashscreen - first Time with shared preferences
Hello I would like to display an other splashscreen after the default one, if the App starts for the first time(right after installation eg) 您好,我想在默认启动画面之后显示另一个启动画面,如果该应用程序是首次启动(例如,安装后立即启动)
So I wrote this. 所以我写了这个。 But the new Activity does not start it stays at the Splash screen.
但是新的活动未启动,它停留在“启动”屏幕上。 Can somebody say whats wrong with it?
有人可以说出什么毛病吗?
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
public class splash extends Activity {
private Thread splashTread;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false)) {
// run your one time code
Intent i = new Intent(splash.this, main.class);
startActivity(i);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
// thread for displaying the SplashScreen
splashTread = new Thread() {
@Override
public void run() {
try {
synchronized(this){
//wait 2 sec
wait(2000);
}
} catch(InterruptedException e) {}
finally {
finish();
//start a new activity
Intent i = new Intent();
i.setClass(splash.this, main.class);
startActivity(i);
stop();
}
}
};
splashTread.start();
}
}
}
Thanks. 谢谢。
From what I've seen, your code runs the same Activity (main) regardless of whether the launch is the first time or not. 从我所看到的,无论启动是第一次还是第一次,您的代码都运行相同的Activity(主)。 I'm assuming your purpose is to immediately launch the alternate splash screen if it's the first launch, otherwise to proceed to the main Activity after 2 seconds.
我假设您的目的是如果是第一次启动,则立即启动备用启动屏幕,否则在2秒钟后进入主活动。 Also, I would recommend using a Handler rather than a Thread, since you're only making use of it once, and on a delay.
另外,我建议使用处理程序而不是线程,因为您只会使用一次,并且会延迟使用。 Try this:
尝试这个:
public class splash extends Activity
{
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
Intent i = new Intent(splash.this, main.class);
splash.this.startActivity(i);
this.finish()
}
};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false))
{
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
Intent i = new Intent(splash.this, otherSplash.class);
this.startActivity(i);
this.finish();
}
else
{
this.setContentView(R.layout.splash);
handler.sendEmptyMessageDelayed(0, 2000);
}
}
}
Instead of calling finish()
... just start the main activity with FLAG_ACTIVITY_CLEAR_TOP
而不是调用
finish()
...只需使用FLAG_ACTIVITY_CLEAR_TOP
开始主要活动
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
Here's more information on that FLAG 这是有关该标志的更多信息
FLAG_ACTIVITY_CLEAR_TOP in Android Android中的FLAG_ACTIVITY_CLEAR_TOP
To be clearer, what I'm suggesting is that you either 更清楚地说,我的建议是
Splash
the default activity and in there call the Main
activity after a timeout, OR, if the splash has already been seen (preference check). Splash
设为默认活动,并在超时后在其中调用Main
活动,或者,如果已经看到Splash
(首选项检查)。 (ie, all splash logic is in the Splash
activity) Splash
活动中) Main
activity check to see if Splash
should be called (preference check) and if so start it using the same CLEAR_TOP
flag and then have the Splash
timeout and set the Main
again using the CLEAR_TOP
after a couple of seconds. Main
活动检查以查看是否应调用Splash
(首选项检查),然后使用相同的CLEAR_TOP
标志启动它,然后使Splash
超时,并在几秒钟后使用CLEAR_TOP
再次设置Main
。 (This mixes the splash logic in both the Splash
and the Main
) Splash
和Main
混合了Splash
逻辑) The end result is that Main
will be the only activity on the stack, once the Splash
is done. 最终结果是,一旦完成
Splash
, Main
将是堆栈上唯一的活动。
If your intention is to simply have multiple "Splash" screens, and they have no logic to them, other than just displaying content, then why not just use a single activity and replace the view with a new view for the second splash. 如果您只是想拥有多个“闪屏”屏幕,而除了显示内容之外,它们就没有逻辑,那为什么不只使用一个活动,而将第二个闪屏替换为新的视图。 You'd have to do this update on the UI thread as well, so you'd need to either use the an
Handler
or use and AsyncTask
, or use View.post()
from a known view element in the current view. 您还必须在UI线程上进行此更新,因此您需要使用
Handler
或使用and AsyncTask
,或者使用当前视图中已知视图元素中的View.post()
。
I don't think you can/should start activity from a thread that is not the UI thread. 我认为您不能/应该从不是UI线程的线程开始活动。 Change to an AsyncTask, or use a Handler
更改为AsyncTask或使用处理程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.