只显示一次启动画面

时间:2013-12-17 18:07:32

标签: android android-layout

我有一个收音机aac播放器,我在开头添加了一个启动画面,但我想只显示一次,因为如果用户按下后退按钮,我的应用程序会停留在播放音乐服务的背景上,但是当我走的时候回到应用程序再次显示启动画面。这是我的实际启动画面代码:

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

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

我可以更改或添加什么才能在首次启动应用时显示启动画面?

3 个答案:

答案 0 :(得分:2)

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

实施服务的onDestory活动和ScreenTabs方法的onDestroy,然后

@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

答案 1 :(得分:1)

使用布尔类型添加共享首选项并将其设置为false并在第一次启动页面后将其设置为true,这样就不会进入该方法 -

开始行动 -

SharedPreferences sharedPref;
    Context context;
    boolean isScrennoFill = false;

以下OnCreate() -

context = this;
        /**
         * get user login preference
         */
        sharedPref = context.getSharedPreferences("savecredentails",
                MODE_PRIVATE);
        isScrennoFill = sharedPref.getBoolean("isScrennoFill", false);

if (isScrennoFill == false) {

                        Intent intent = new Intent(context,
                                SplashPage.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(context,
                                Next.class);
                        startActivity(intent);
                    }

答案 2 :(得分:0)

在您的AndroidManifest.xml中,您可以在要禁用的活动中使用android:noHistory="true"返回