2013-12-17 4 views
0

У меня есть плеер aac для радио. Я добавил экран заставки в начале, но я хотел бы показать его только один раз, потому что если пользователь нажмет кнопку «Назад», мое приложение останется на фоне с музыкальным сервисом но когда я вернусь в приложение, снова появится экран заставки. Вот мой фактический код SplashScreen:показать заставку только один раз

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

OnDestroy из 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(); 
     } 
    } 
} 

Что я могу добавить или изменить для того, чтобы показать заставку только первое приложение время запускается?

+0

Я установил время обработчика 0s, чтобы избежать повторения экрана всплеска. –

+0

Можете ли вы привести пример в моем коде? – alexistkd

+0

использовать shref pref, инициализировать его, а затем проверять значение каждый раз, когда его start.if valye больше, чем ваше значение, а затем переходите к следующему экрану. – Dev

ответ

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

Что мы делаем здесь, что значение предпочтения first_time, который проверяет, что Splash должен быть показан или нет, установлен на true только w если действие ScreenTabs завершено или музыкальная игра остановлена.

+0

, пожалуйста, дайте мне пример, я тоже вижу его :) – alexistkd

+0

im get error with editor.putBoolean ("show_splash", false); и if (! prefs.getBoolean («first_time», false)) – alexistkd

+0

@alexistkd увидит меня, изменил ответ, поймите его, прежде чем выполнять его. –

1

Добавить общее предпочтение с логическим типом и сделать его ложным и после страницы заставки первый раз сделать это верно, так что будет не идти в этом ме-

на старте Actitity-

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

может у вас привести пример, используя мой фактический код? – alexistkd

+0

не работает для меня, краш приложения – alexistkd

+0

он должен работать..пожалуйста отправьте свой логарифм. –

0

в вашем AndroidManifest.xml вы можете использовать android:noHistory="true" в деятельности вы хотите отключить вернуться к

+0

не повезло с этим – alexistkd

+0

действительно? Почему это дало вам ошибку или что-то в этом роде? –

+0

Ошибка не приводит к показу заставки – alexistkd

Смежные вопросы