共享preference与闪屏 [英] sharedpreference with splash screen
问题描述
我的应用程序通过闪屏的音乐开始,我使用的共享preference停止音乐,所以下次打开该应用程序启动画面还有没有音乐的时间。
我尝试获得preference屏幕采用三个独立的不同的检查框功能,如果你还选一checkedbox不能签其他两个如下:
第一checkedbox:开始启动画面和音乐(由下面code实现的)应用程序,
二checkedbox:开始闪屏和没有音乐(由下面code实现的)应用程序,
第三checkedbox:开始应用,而无需启动画面和音乐(未实现)
任何帮助将AP preciated,谢谢
在code:
开机:
公共类飞溅延伸活动{
MediaPlayer的ourSong;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.splash); ourSong = MediaPlayer.create(Splash.this,R.raw.splashsound); 共享preferences的get preFS = preferenceManager.getDefaultShared preferences
(getBaseContext());
布尔音乐= GET prefs.getBoolean(复选框,真正的);
如果(音乐== true)而
ourSong.start(); 螺纹定时器=新的Thread(){
公共无效的run(){
尝试{
睡眠(1000); }
赶上(InterruptedException的E){
e.printStackTrace(); }
最后{
意图openMainActivity =新意图(com.test.demo.MENU);
startActivity(openMainActivity); }}
};
timer.start(); }@覆盖
保护无效的onPause(){
// TODO自动生成方法存根
super.onPause();
ourSong.release();
完();
}
}
preFS:
公共类preFS延长$ P $ {pferenceActivity@燮pressWarnings(德precation)
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
布尔customTitleSupported = requestWindowFeature
(Window.FEATURE_CUSTOM_TITLE);
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
加preferencesFromResource(R.xml preFS); }
}
prefs.xml:
<?XML版本=1.0编码=UTF-8&GT?;
< preferenceScreen的xmlns:机器人=http://schemas.android.com/apk/res/android>
<复选框preference机器人:标题=闪屏音乐
机器人:设置defaultValue =真
机器人:键=复选框
机器人:总结=删除标记停止音乐时飞溅开始/>
< / preferenceScreen>
我达到了什么我寻找,这是三个不同的功能checkedbox,如下code半个解决方案,但我还是不能让他们独立检查。
公共类飞溅延伸活动{
MediaPlayer的ourSong;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.splash); 共享preferences的get preFS = preferenceManager.getDefaultShared preferences(getBaseContext()); 布尔without_splash_screen = GET prefs.getBoolean(without_splash_screen,真正的);
如果(without_splash_screen ==真)
{
意向意图=新意图(Splash.this,MainActivity.class);
startActivity(意向);
} 布尔飞溅= GET prefs.getBoolean(扑通一声,真);
如果(闪==真){
的setContentView(R.layout.splash);
螺纹定时器=新的Thread()
{
公共无效的run()
{
尝试
{
睡眠(2000);
}
赶上(InterruptedException的E)
{
e.printStackTrace();
}
最后
{
意向意图=新意图(Splash.this,MainActivity.class);
startActivity(意向);
}
}
};
timer.start();
} ourSong = MediaPlayer.create(Splash.this,R.raw.splashsound); 共享preferences的get preFS1 = preferenceManager.getDefaultShared preferences(getBaseContext());
布尔音乐= GET prefs1.getBoolean(splash_music,真正的);
如果(音乐== true)而
ourSong.start(); 螺纹定时器=新的Thread(){
公共无效的run(){
尝试{
睡眠(2000); }
赶上(InterruptedException的E){
e.printStackTrace(); }
最后{
意向意图=新意图(Splash.this,MainActivity.class);
startActivity(意向); }}
};
timer.start(); }@覆盖
保护无效的onPause(){
// TODO自动生成方法存根
super.onPause();
ourSong.release();
完();
}
}
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>
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();
}
}
这篇关于共享preference与闪屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!