5

My app start by splash screen with music , i used sharedpreference to stop music so next time you open the app splash screen still there without music .

im trying to get preference screen with three independent different checked box functions and also if you check one checkedbox you can not check the other two as below :

First checkedbox: start app with splash screen and music ( achieved by below code ) ,

Second checkedbox: start app with splash screen and without music ( achieved by below code ) ,

third checkedbox: start app without splash screen and music ( not achieved ) .

any help will be appreciated , thanks

the code :

Splash :

 public class Splash extends Activity{  
    MediaPlayer ourSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
         setContentView(R.layout.splash);  

    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 

    SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences
              (getBaseContext());
    boolean music = getPrefs.getBoolean("checkbox", true);
    if (music == true)      
    ourSong.start();

    Thread timer = new Thread(){
    public void run(){
        try{
            sleep(1000); }
          catch (InterruptedException e){
            e.printStackTrace(); }
          finally{
        Intent openMainActivity = new Intent("com.test.demo.MENU");
                startActivity(openMainActivity); }}                                 
                                    };
                timer.start();   }

@Override
protected void onPause() {
            // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
          } 
       }

Prefs :

public class Prefs extends PreferenceActivity{

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    Boolean customTitleSupported = requestWindowFeature
                 (Window.FEATURE_CUSTOM_TITLE);    
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs); 

                                          }
                           }

prefs.xml:

  <?xml version="1.0" encoding="utf-8" ?> 
     <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
          <CheckBoxPreference android:title="splash screen music" 
                  android:defaultValue="true" 
                  android:key="checkbox" 
                  android:summary="remove mark to stop music when splash start" /> 
     </PreferenceScreen>
androidqq6
  • 1,526
  • 2
  • 22
  • 47
  • 1
    Do you want to write inside the SharedPreferences, if your application should start with the splashscreen or not? – Blackbelt Jul 04 '13 at 10:29
  • @blackbelt yes ,ex. like have 3 checkboxs , if checked first one so app will start with splash and music , if second checked app start with splash witout music and if thitd checked app start without splash and without music , finally one checkedbox only to be checked , thanks – androidqq6 Jul 04 '13 at 10:42
  • is in Prefs that you want to write those values inside sharedpreferences? – Blackbelt Jul 04 '13 at 10:44
  • @blackbelt yes my dear – androidqq6 Jul 04 '13 at 10:54
  • 2
    @blackbelt my dear is there is another way to achive 3 checked box with different function related to my post than what i expected , thanks alot – androidqq6 Jul 06 '13 at 19:44
  • if I even install your app be sure I will hate you for splash music – Marcin Orlowski Jul 07 '13 at 19:40
  • @Marcin Orlowski your welcome my friend , so after you open it just uncheck the checkedbox so it will start next time without music , even if you want to listen to music , its nice classic music for buddha bar . :)) – androidqq6 Jul 07 '13 at 20:17
  • 1
    @androidqq6 Unless your app is something amazing, there most likely will be no "next time" for your app :) – Marcin Orlowski Jul 07 '13 at 20:20

6 Answers6

1
    SharedPreferences getPrefs =PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean music = getPrefs.getBoolean("checkbox");
    if (music == true)  
    {
        setContentView(R.layout.splash);  
        ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);        
        ourSong.start();
        Thread timer = new Thread()
        {
            public void run()
            {
                try
                {
                    sleep(1000); 
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace(); 
                }
                finally
                {
                    Intent openMainActivity = new Intent("com.test.demo.MENU");
                    startActivity(openMainActivity); 
                }
            }                          
        };
        timer.start();   
    }
}
else
{
    Intent openMainActivity = new Intent("com.test.demo.MENU");
    startActivity(openMainActivity);
}
Zala Janaksinh
  • 2,929
  • 5
  • 32
  • 58
prvn
  • 916
  • 5
  • 5
  • and also add getPrefs.setBoolean("checkbox",false); in your onpause – prvn May 14 '13 at 06:45
  • my friend the above answered code result in two option :1-if checkbox lead to start splash with music or 2- if not checked the app start with out splash and music at all but i want to add another option which is start splach without music so finally we have 3 option :1- without splash and music .2-with splash but without music .3-with splash and music , please how we can achieve that , thanks alot – androidqq6 Jul 02 '13 at 13:07
1

The only thing you need to do is add another CheckBox to your preferences that way:

prefs.xml

  <?xml version="1.0" encoding="utf-8" ?> 
     <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <CheckBoxPreference android:title="splash screen" 
                android:defaultValue="true" 
                android:key="splash" 
                android:summary="Start a spashscreen" />
        <CheckBoxPreference android:title="splash screen music" 
                  android:defaultValue="true" 
                  android:key="splash_music" 
                  android:summary="Add some music to the splashscreen" /> 
     </PreferenceScreen>

And now, you just need to check the value of "splash" to know if you need to show the splashscreen. If it equals to false, just open the new activity:

Splash

 public class Splash extends Activity{  
    MediaPlayer ourSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
         setContentView(R.layout.splash);  



    boolean splash = getPrefs.getBoolean("splash", true);
    if(!splash) {
        Intent openMainActivity = new Intent("com.test.demo.MENU");
        startActivity(openMainActivity);
    }
    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 

    SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences
              (getBaseContext());
    boolean music = getPrefs.getBoolean("splash_music", true);
    if (music == true)      
    ourSong.start();

    Thread timer = new Thread(){
    public void run(){
        try{
            sleep(1000); }
          catch (InterruptedException e){
            e.printStackTrace(); }
          finally{
        Intent openMainActivity = new Intent("com.test.demo.MENU");
                startActivity(openMainActivity); }}                                 
                                    };
                timer.start();   }

@Override
protected void onPause() {
            // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
          } 
       }

EDIT: A few tips since I saw you were just beginning in Android Development:

Anyway, good luck with your project(s)!

Community
  • 1
  • 1
MagicMicky
  • 3,819
  • 2
  • 37
  • 53
  • i have option menu of 3 button(about,pref,exit),when i install app and open pref, default both checkedbox was checked, running app with splash and music,suppose you unchecked first option to disable splash or unchecked both splash and add music then press back then press exit,it exit the app,then reopen app again and press pref and enable splash or enable splash and music then press back then press exit( here is the problem),option menu disappear but app doesnt exit you have to press pref again to rise option menu then press exit again now it exit the app any advice solve it,thanks – androidqq6 Jul 07 '13 at 23:04
  • See this link : http://developer.android.com/guide/topics/manifest/activity-element.html#nohist, might solve your problem to put the `noHistory` flag to your splashscreen activity. But I'm not sure since I don't really understand your problem here! – MagicMicky Jul 07 '13 at 23:31
  • i mean after check splash to be enable exit button not respond to click from first click , you must click it twice to exit the app , i will try onHistory to solve it – androidqq6 Jul 08 '13 at 06:16
  • android:noHistory="true" doesn't solve it , any advice ,thanks – androidqq6 Jul 08 '13 at 13:11
  • Well, it might be another problem then. You should open another thread and be more explicit about this issue: what's really happening, show your AndroidManifest... – MagicMicky Jul 08 '13 at 16:22
  • my dear that issue was not exit in my code after i apply your answer code its appear also i can remove exit button from my app but still back botton doesnt respond to first click you have to click twice to get back , please help me in solve that – androidqq6 Jul 08 '13 at 22:40
  • also if you look to my post again i need independant 3 checked box with different functions as: First checkedbox: start app with splash screen and music , Second checkedbox: start app with splash screen and without music , third checkedbox: start app without splash screen and music . – androidqq6 Jul 08 '13 at 22:55
  • @ MagicMicky and also if you check one checkedbox you can not check the other two , thanks alot – androidqq6 Jul 08 '13 at 22:57
1

I would suggest you to replace checkbox with radios to achive point 3.

Like this:

<?xml version="1.0" encoding="utf-8" ?> 
     <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
         <ListPreference
                        android:title="Splash settings"
                        android:summary="Select the way splash works"
                        android:key="splashPref"
                        android:defaultValue="0"
                        android:entries="@array/listArray"
                        android:entryValues="@array/listValues" />
    </PreferenceScreen> <!-- you can use anything as value - int or string -->

And then in your code:

...
int splashType = getPrefs.getInt("splashPref", 0);
switch (splashType) {
case 2:
   // skip splash
case 1:
   // only splash without music
default:
case 0:
    // splash with music
}

To get better code readability I would suggest you create enum that you will create from shared prefences value, like:

public enum SplashPreferenceValue {
    SPLASH_WITH_MUSIC(0),
    SPLASH_WITHOUT_MUSIC(1),
    NO_SPLASH(2);


    private int value;

    private SplashPreferenceValue(int value) {
        this.value = value;
    }

    public static SplashPreferenceValue fromInt(int value) {
        return new SplashPreferenceValue(value);
    }
};


...


SplashPreferenceValue splashType = SplashPreferenceValue.fromInt(getPrefs.getInt("splashPref", 0));

switch (splashType) {
case NO_SPLASH:
   // skip splash
   break;
case SPLASH_WITHOUT_MUSIC:
   // only splash without music
   break;
default:
case SPLASH_WITH_MUSIC:
    // splash with music
    break;
}
dant3
  • 966
  • 9
  • 26
  • my dear where should i put the above class , in splash class or Prefs class , thanks – androidqq6 Jul 08 '13 at 13:21
  • @androidqq6 do you mean enum? It's up to you to decide there you want to place it. Looking at your current approach I would suggest you to create separate .java file for it. – dant3 Jul 10 '13 at 13:11
  • my dear im sorry but im new to android and i dont know how to apply your code can you explain more or rearange your code and where and how to apply it ,thanks alot – androidqq6 Jul 11 '13 at 19:06
1

i reach to half solution of what i seek , which is three different function checkedbox , as code below but still i cant get them independent check .

public class Splash extends Activity{   
MediaPlayer ourSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
         setContentView(R.layout.splash);  

         SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());                 

         boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
            if (without_splash_screen == true)
            {   
                Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                startActivity(intent);
            }

    boolean splash = getPrefs.getBoolean("splash", true);       
    if(splash == true) {
        setContentView(R.layout.splash);  
        Thread timer = new Thread()
        {
            public void run()
            {
                try
                {
                    sleep(2000); 
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace(); 
                }
                finally
                {
                    Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                    startActivity(intent);  
                }
            }                          
        };
        timer.start();   
    }                 

    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 

    SharedPreferences getPrefs1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean music = getPrefs1.getBoolean("splash_music", true);
    if (music == true)      
    ourSong.start();

    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(2000); }
              catch (InterruptedException e){
                e.printStackTrace(); }
              finally{
                  Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                  startActivity(intent); }}                                 
                                };
         timer.start();   }

@Override
protected void onPause() {
            // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
          } 
       }
androidqq6
  • 1,526
  • 2
  • 22
  • 47
0

From what I can understand you want the following to happen:

A. user selects one of the checkboxes then the other two are set to false

B. when the app starts it checks the preferences and will play/not play music, show/not show the splash screen.

To achieve A you need to use the onPreferenceChange method. In this method you will check if the preference changed is true, if it is then set the other two preferences to false. The problem comes if all three preferences are false in which case you need to either set a default preference, or perform a default action in the Splash Activity class.

That should take care of ensuring only one preference is set to true. I would put the three preferences in a preference category to show the user they are grouped. The preferred method to handle this case would be through a radio button group which would always ensure one option is always selected.

B. In your Splash activity I would extract the call to start the next activity to a separate method (move finish() call to this method) and then in the onCreate() method check the preference settings. If the preference is to skip the splash make a call to the startNextActivity method, otherwise enter the wait thread and continue to the onResume() method with the appropriate music settings.

You haven't included any onResume() code in your sample code, this is where you should be starting the music. Also I would move the call to stop the music and finish() to the extracted startNextActivity() method.

Also instead of using a Thread to run the timer use something like this which will help keep things smooth and move the timer off the main thread.

new Handler().postDelayed(new Runnable() {
    public void run() {
        startNextActivity();
    }
}, SPLASH_DISPLAY_LENGTH);

Hope that helps.

Phil H
  • 897
  • 4
  • 10
0

Never use sleep method in activity. The Shared Preference in android is used to store key-value pair. You can store integers, Boolean, string, float, etc in Shared Preference. All you need to do is just give a name (key) to the value(value) that you wants to store. There you will preform all the share preference task using shared preference editor only. So the process is – initialize editor, store key-value and call editor.commit() to store all the changes.

I hope you get the idea about share preference. So our task is to show splash screen on app start. Splash screen will be shown to user for 3-5 seconds then it will show login screen or direct home screen (if user already sign-in ). So how will the system decide that the user is already sign-in OR it is first visit of a new user? Please Check complete code here

You should use user_data as a key and when user will get successful login(sign-in), store the user id in user_data (key).Using IF condition I can easily identify that user_data (key) is empty or not. If the user_data (key) has some value that means user has already sign-in, So let the user into HOME Screen otherwise show login screen.

package blueappsoftware.shopeasy.splash;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import blueappsoftware.shopeasy.R;
import blueappsoftware.shopeasy.Utility.Constant;
import blueappsoftware.shopeasy.Utility.SharePreferenceUtils;
import blueappsoftware.shopeasy.home.HomeActivity;
import blueappsoftware.shopeasy.login.SigninActivity;
/**
 * Created by kamal_bunkar on 03-12-2018.
 */
public class SplashActivity extends AppCompatActivity {
    private String TAG ="splashAcctivity";
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        init();
       // Log.e(TAG, " splash start showing");
    }
    public void init(){
        new Handler().postDelayed( new Runnable() {
            @Override
            public void run() {
                /// if user registered user
                // then show home screen
                // else  show login screen
                // key  register_user
              //  Log.e(TAG, "  counter running ");
                if (SharePreferenceUtils.getInstance().getString(Constant.USER_DATA).equalsIgnoreCase("")){
                    // not registted user  so show login screen
                    Intent intent = new Intent(SplashActivity.this, SigninActivity.class);
                    startActivity(intent);
                }else {
                    // home sscreen
                    Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
                    startActivity(intent);
                }
                finish();
            }
        }, 3000 );
    }
}
Kamal Bunkar
  • 1,354
  • 1
  • 16
  • 20