Sharedpreference with screensaver

My application starts from the splash screen with music, I used sharedpreference to stop the music, so the next time you open the splash screen of the application, it’s still there without music.

im trying to get a preferences screen with three independent different checkboxes, and also if you check one box, you cannot check the other two as shown below:

The first flag: start the application with a screensaver and music (achieved below the code),

The second flag: start the application with a splash screen and without music (reached below the code),

third flag: start the application without a screensaver and music ( not achieved ).

any help would be appreciated thanks

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> 
+4
source share
6 answers

I get to half the solution of what I'm looking for three different checkbox functions, like the code below, but still I can not get an 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(); } } 
+1
source
  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); } 
+1
source

The only thing you need to do is add another CheckBox to your preferences as follows:

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 β€œsplash” value to see if you want to show the popup. If it is false, just open a new action:

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 that you are just getting started in Android Development:

Anyway, good luck with your project (s)!

+1
source

I would suggest that you replace the check box with the radio to reach 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 readability of the code, I would suggest you create an enumeration that you will create from the general value of privileges, for example:

 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; } 
+1
source

From what I can understand, you want the following:

a. the user selects one of the flags, and the other two - false

V. when launching the application, it checks the preferences and will play / not play music, show / not show the screensaver.

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

This should make sure that one preference is set to true. I would put three preferences in the preference category to show the user with whom they are grouped. The preferred way to handle this case would be a group of switches that would always provide a single option.

C. In your Splash activity, I would retrieve the call to start the next action using a separate method (move finish () for 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 a wait stream and continue the onResume () method with the appropriate music settings.

You did not include onResume () code in the sample code, here you should start the music. I would also move the call to stop the music and end () to the extracted startNextActivity () method.

Instead of using Thread to start the timer, use something like this that will help maintain smoothness and move the timer from the main thread.

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

Hope this helps.

0
source

Never use the sleep method in action. Shared preference in android is used to store key-value pairs. You can store integers, booleans, strings, float, etc. In general preferences. All you have to do is just point the name (key) to the value (value) that you want to save. There you transform the entire preference task using the general preference editor. Thus, the process is to initialize the editor, save the key-value and call editor.commit () to save all the changes.

I hope you understand how to share your preferences. Therefore, our task is to show a pop-up screen when the application starts. The splash screen will be displayed to the user for 3-5 seconds, after which he will display the login screen or direct home screen (if the user is already logged in). So, how does the system decide that the user has already registered or first visited a new user? Please check the full code here.

You must use user_data as the key, and when the user receives a successful login (login), save the user ID in user_data (key). Using the IF condition, I can easily determine if user_data (key) is empty or not. If user_data (key) has some value meaning that the user has already logged in, so let the user go to the "HOME" screen, otherwise display the 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 ); } } 
0
source

Source: https://habr.com/ru/post/1480601/


All Articles