仅显示一次启动画面 [英] show splash screen one time only
问题描述
我有一个广播AAC播放器,我在开始时添加了一个启动屏幕,但是我只想显示一次,因为如果用户按下后退"按钮,我的应用程序会停留在背景上,并且正在播放音乐服务,但是当我回到应用程序再次显示启动屏幕.这是我实际的启动画面代码:
I have a radio aac player I added a splash screen at the beginning, but I would like to show it just one time, because if user press back button my app stays on background with a music service playing, but when I go back to the app shows splash screen again. Here is my actual splashscreen code:
public class Inicio extends Activity {
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
Intent i = new Intent(Inicio.this, ScreenTabs.class);
Inicio.this.startActivity(i);
Inicio.this.finish();
}
};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false))
{
/*
// we will set this true when our ScreenTabs activity
ends or the service playing music is stopped.
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
*/
Intent i = new Intent(Inicio.this, ScreenTabs.class);
this.startActivity(i);
this.finish();
}
else
{
this.setContentView(R.layout.inicio);
handler.sendEmptyMessageDelayed(0, 2000);
}
}
}
screentabs.java的毁灭
Ondestroy of screentabs.java
@Override
protected void onDestroy() {
super.onDestroy();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
if(radioService!=null) {
if(!radioService.isPlaying() && !radioService.isPreparingStarted()) {
//radioService.stopSelf();
radioService.stop();
radioService.stopService(bindIntent);
radioService.exitNotification();
}
}
}
为了在首次启动应用程序时显示启动屏幕,我可以更改或添加什么?
What can I change or add in order to show splash screen just first time app is initiated?
推荐答案
public class Inicio extends Activity {
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
Intent i = new Intent(Inicio.this, ScreenTabs.class);
Inicio.this.startActivity(i);
Inicio.this.finish();
}
};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false))
{
/*
// we will set this true when our ScreenTabs activity
ends or the service playing music is stopped.
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
*/
Intent i = new Intent(Inicio.this, ScreenTabs.class);
this.startActivity(i);
this.finish();
}
else
{
this.setContentView(R.layout.inicio);
handler.sendEmptyMessageDelayed(0, 2000);
}
该服务的ScreenTabs
活动的onDestory
实施和该服务的onDestroy
方法
Implement onDestory
of the ScreenTabs
activity and onDestroy
method of the service and there
@Override
public void onDestory(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
}
以及类似的服务onDestory
@Override
public void onDestory(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
}
我们在这里所做的操作是,只有在ScreenTabs活动结束或音乐播放服务停止后,用于检查是否显示Splash的首选项值first_time才设置为true
.
What we do here that the preference value first_time which checks that Splash should be shown or not is set to true
only when the ScreenTabs activity is finished or the music playing service is stopped.
这篇关于仅显示一次启动画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!