将sharedPreferences添加到listpreference值

时间:2013-07-16 00:23:44

标签: android sharedpreferences listpreference

我的应用程序以Splash屏幕开始,然后转到MainActivity取决于用户首选项,该首选项由listpreference中的任何选项确定:

1-启动应用程序,没有启动和音乐。

2-启动应用程序只有启动。

3-启动应用程序,带有启动和音乐。

我实现了listpreference,但我不能将sharedPreferences添加到每个值,所以在检查了它们中的任何一个之后它存储在sharedPreferences中并启动应用程序取决于用户首选项,

因为这是第一次使用listpreference而我是新的android我不知道怎么做,

任何帮助都将不胜感激。

Splash.java

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

Prefs.java

public class Prefs extends PreferenceActivity{

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // 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">

     <ListPreference
       android:title="Application Start Preference"
       android:summary="This preference allows to select how the app will start"
       android:key="listPref"
       android:entries="@array/splash"
       android:entryValues="@array/splash_values" />          
</PreferenceScreen>

arrays.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>   
  <string-array name="splash">
      <item>start app without splash screen </item>
      <item>start app with splash scrren only</item>
      <item>start app with splash screen and music</item>
  </string-array>

  <string-array name="splash_values">
      <item>1</item>
      <item>2</item>
      <item>3</item>

  </string-array>    
</resources>

更新 我试过下面的代码,但是发生的是应用程序运行正常启动与启动然后转到mainactivity然后我按选项菜单中的首选项它确定它打开首选项页面然后按下listpreference它上升3 radiobutton的对话框,如果我按任何一个radiobuttons它没关系,然后我回来然后回到退出应用程序,然后再次打开它直接打开显示主要活动没有飞溅然后立即崩溃。

logcat中的第23行是:

 int splashType = getPrefs.getInt("listPref", 0);

Splash.java

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

 int splashType = getPrefs.getInt("listPref", 0);
  switch (splashType) {
  case 2:
Intent intent = new Intent(Splash.this, MainActivity.class);                                     
       startActivity(intent);
  break;

  case 1:
   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();       
break;

 case 0:
   ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 
   ourSong.start();

Thread timer1 = new Thread(){
    public void run(){
        try{
            sleep(2000); }
          catch (InterruptedException e){
            e.printStackTrace(); }
          finally{
          Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                startActivity(intent); 
                    }
                   }                                    
                 };
           timer1.start();   
  break;
   }
  }
@Override
protected void onPause() {
     // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
      } 
   }

logcat的:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tsn.dr/com.tsn.dr.Splash}: java.lang.ClassCastException: java.lang.String
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: java.lang.String
at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2968)
at com.tsn.dr.Splash.onCreate(Splash.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

1 个答案:

答案 0 :(得分:0)

你可以尝试类似的东西来获取存储的用户偏好,然后采取相应的行动:

SharedPreferences sharedPrefs;
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String userSplashValue = sharedPrefs.getString("listPref", "1");

if (userSplashValue.equals ("1")) {
 // choice 1
}
else if (userSplashValue.equals("2")) {
 // choice 2    
}
else if (userSplashValue.equals ("3")){
 // choice 3    
}