问题描述
我有下面的类, SplashActivity.java
public class SplashScreen extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
}catch(InterruptedException e)
{
e.printStackTrace();
}
finally{
Intent tutorial = new Intent(SplashScreen.this, TutorialOne.class);
startActivity(tutorial);
}
}
};
timer.start();
}
}
我想这个活动只加载一次,当应用程序第一次安装,第一次在移动设备上。作为新到Android我对这个非常小的想法。我读的地方,该共享preferences
将被使用,但不明白的实施。而关于本次活动的一点是,该活动已经作为一个启动
首次使用的时候,这就是真正搞糊涂了。因为在清单文件我声明另一个活动这在我的情况下,将是 MainPage.java
。那么,如何实现这个逻辑?难道我在 MainPage的
呼吁 SplashActivity
还是有别的东西是必须做的?请帮助别人?
I want this activity to load only once, when the app is first installed on the mobile device for the first time. Being new to android I have very little idea about this. I read in places that the SharedPreferences
is to be used, but did not understand the implementation. And the thing about this activity is that, the activity has to act as a Launcher
when used for the first time, that's what really confused me. Because in the manifest file I am declaring another activity which in my case would be the MainPage.java
. So how can I implement this logic ?? Do I call upon the SplashActivity
in the MainPage
or is there something else that must be done ?? Please help someone ?
有人请写下code。如果可以实现这个逻辑?在先进的感谢。
Can someone please write down the code to implement this logic if possible ?? Thanks in advanced.
推荐答案
将此code到您的onCreate方法
Add this code to your onCreate method
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
if(pref.getBoolean("activity_executed", false)){
Intent intent = new Intent(this, TutorialOne.class);
startActivity(intent);
finish();
} else {
Editor ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
}
共享preferences将继续在每次执行,除非你清理你的Andr oid从设置中的数据的应用程序的时间。第一次是打算从一个boolean(activity_executed)保存在这样的preferences(活动preF)时获得的价值。
SharedPreferences will be keep every time you execute the app unless you clean the data from Settings on your Android.The first time is going to get the value from a boolean (activity_executed) saved on such preferences (ActivityPREF).
如果没有找到任何值将返回假的,所以我们要编辑preference并将其值设置为true。接下来的执行将推出活动 TutorialOne
。
If it does not find any value it will return false, so we have to edit the preference and set the value to true.The next execution will launch the activity TutorialOne
.
完成()
删除从堆栈历史记录当前的活动,所以没有可能是用按键回程从TutorialOne回来。
finish()
erases the current activity from the stack history, so no come back is possible using button back from TutorialOne.
关于你的清单,你可以把这个actitiy与
About your manifest, you can set this actitiy with
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
每一个应用程序被执行将推出这个活动的时间,但由于对activity_executed
将要开始一个新的活动<$ C $的真实设置好的C> startActivity 。
Every time the app is executed will launch this activity, but due to the true setted on the "activity_executed"
is going to start a new activity with startActivity
.
这篇关于制作活动只出现一次,当应用程序启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!