gpt4 book ai didi

android - 启动画面 - 首次共享首选项

转载 作者:太空狗 更新时间:2023-10-29 16:22:32 26 4
gpt4 key购买 nike

你好我想在默认启动画面之后显示另一个启动画面,如果应用程序是第一次启动(例如安装后立即启动)

所以我写了这个。但是新的 Activity 不会启动,它会停留在启动画面上。有人能说说它有什么问题吗?

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

}

}
}

谢谢。

最佳答案

据我所见,无论是否是第一次启动,您的代码都运行相同的 Activity(主)。我假设您的目的是在第一次启动时立即启动备用启动画面,否则在 2 秒后继续主 Activity。另外,我建议使用处理程序而不是线程,因为您只使用它一次,而且会延迟使用。试试这个:

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

}
}

关于android - 启动画面 - 首次共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11042302/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com