2013-07-16 60 views
0

我的應用程序與閃屏開始然後去MainActivity取決於其選擇在listpreference任何選項來確定用戶的偏好:添加sharedPreferences到listpreference值

1-開始應用,但不飛濺和音樂。

2-啓動應用程序只有飛濺。

3-啓動應用程序與飛濺和音樂。

我實現listpreference,但我不能添加sharedPreferences每個值,以便檢查後其中的任何它存儲在sharedPreferences和啓動應用程序依賴於用戶的喜好,

,因爲這是第一次使用listpreference和IM新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> 

UPDATE: 我想下面的代碼,但最新發生的是應用程序的正常運行飛濺開始,然後去mainactivity然後我按偏好選項菜單中的OK它打開首選項頁面,然後按listpreference它上升3個單選按鈕的對話框,如果我按任何一個單選按鈕它的確定,然後我退後,直到退出的應用程序,然後再次打開它直接打開顯示主要活動沒有飛濺,然後立即崩潰。

在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) 
+0

什麼是'Splash'中的第23行。另外,我不清楚發生了什麼。你點擊首選項,然後崩潰? – codeMagic

+0

@codeMagic即時通訊對不起:最新發生的應用程序運行正常開始與飛濺然後去主要活動,然後我在選項菜單中按優先選擇它確定它打開首選項頁然後按列表首選它上升3 radiobutton dialoge,如果我按任何任何單選按鈕其確定,然後我退回到出口應用程序,然後再次打開它直接打開mainactivity沒有飛濺然後崩潰在這裏還行23是:int splashType = getPrefs。getInt(「splashPref」,0); – androidqq6

回答

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

它不工作,任何選項,你按列表首選項,然後回去或退出應用程序,然後直接顯示啓動屏幕和堆棧,謝謝 – androidqq6

+0

因此,你不再有拋出的RunTimeException?你可以發佈你的新代碼,並確保你使用的是正確的密鑰 - 在你的情況下它是「listPref」而不是「splashPref」 –

+0

我調整你的代碼的一小部分代碼,但最後它的工作,謝謝許多。 – androidqq6