本文介绍了如何设置共享preferences其他活动的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序的两项活动。第一个活动闪屏 SplashActivity 。我有共享preferences 闪屏,但我想设置这个真值 SplashActivity 。我想,如果有可能创造 SplashActivity 方法,该方法只运行一次,即这种方法比较的布尔价值闪屏(这样是错误的开始)。后第一次运行它设置为true永远和这个方法被跳过。我已经尝试了很多事情要做这个,但没有成功。

闪屏 -

 公共类闪屏扩展活动
{
    / **第一次创建活动时调用。 * /
    TimerTask的任务;
    共享preferences preF;
    共享preferences.Editor版;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
       super.onCreate(savedInstanceState);
       的setContentView(R.layout.splash_screen);
       preF = getShared preferences(活动preF,Context.MODE_PRIVATE);
       Log.v(,的onCreate呼吁);
       如果(pref.getBoolean(activity_executed,FALSE))
       {
            Log.v(,以前好象叫);
            的setContentView(R.layout.splash_screen);
            Log.v(,后,如果称为);
            新的处理程序()postDelayed(csRunnable1,5000)。
       }
       其他
       {
          新的处理程序()postDelayed(csRunnable2,5000)。
       }
   }

   可运行csRunnable1 =新的Runnable()
   {
       @覆盖
       公共无效的run()
       {
            意向意图=新的意图(SplashScreen.this,SplashActivity.class);
               startActivity(意向);
               完();

       }
   };

   可运行csRunnable2 =新的Runnable()
    {
       @覆盖
       公共无效的run()
       {
            意向意图=新的意图(SplashScreen.this,LoginActivity.class);
               startActivity(意向);
               完();

       }
   };
}
 

SplashActivity -

 公共类SplashActivity扩展活动实现OnClickListener
{
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.welcome);
        ////////////////////////////////////////////////// ////////////////////
        ////////游戏菜单//////////////////////////////////////// /////////
        按钮playBtn =(按钮)findViewById(R.id.playBtn);
        playBtn.setOnClickListener(本);
        按钮settingsBtn =(按钮)findViewById(R.id.settingsBtn);
        settingsBtn.setOnClickListener(本);
        按钮rulesBtn =(按钮)findViewById(R.id.rulesBtn);
        rulesBtn.setOnClickListener(本);
        按钮exitBtn =(按钮)findViewById(R.id.exitBtn);
        exitBtn.setOnClickListener(本);
    }


    / **
     *监听器为游戏菜单
     * /
    @覆盖
    公共无效的onClick(视图v){
        我的意图;

        开关(v.getId()){
        案例R.id.playBtn:
            //登录后,加载主页
            //Log.d("LOGIN,用户已经开始游戏);
            //获取问题集//
            名单<问题>题= getQuestionSetFromDb();
            //初始化游戏与检索问题集///
            游戏C =新的游戏();
            c.setQuestions(问题);
            c.setNumRounds(getNumQuestions());
            ((CYKApplication)getApplication())setCurrentGame(三);
            //开始游戏吧.. //
            I =新的意图(这一点,QuestionActivity.class);
            startActivityForResult(ⅰ,Constants.PLAYBUTTON);
            完();
            打破;

        案例R.id.rulesBtn:
            I =新的意图(这一点,RulesActivity.class);
            startActivityForResult(ⅰ,Constants.RULESBUTTON);
            完();
            打破;

        案例R.id.settingsBtn:
            I =新的意图(这一点,SettingsActivity.class);
            startActivityForResult(ⅰ,Constants.SETTINGSBUTTON);
            完();
            打破;

        案例R.id.exitBtn:
            完();
            打破;
        }

    }
 

我知道这是我需要在 SplashActivity 编辑,但该行每当我加入这个我的应用程序崩溃。

  ED = pref.edit();
ed.putBoolean(activity_executed,真正的);
ed.commit();
 

解决方案

可能是你正在做的事情是错误的。你可以简单地这样做闪屏

 如果(pref.getBoolean(activity_executed,FALSE))
   {
        Log.v(,以前好象叫);
        的setContentView(R.layout.splash_screen);
        Log.v(,后,如果称为);
        新的处理程序()postDelayed(csRunnable1,5000)。
        pref.edit()putBoolean(activity_executed,真).commit()。
   }
 

或你splashactivity OnCreate中调用这个

  getShared preferences(活动preF,Context.MODE_PRIVATE).edit()putBoolean(activity_executed,真).commit()。
 

I have two activities in my app. First activity SplashScreen and other SplashActivity. I have sharedPreferences in SplashScreen but i want to set value of this true in SplashActivity. I think If it is possible to create a method in SplashActivity which run only once i.e this method compare the boolean value of SplashScreen (like this is false at start). After first run its set to true forever and this method is skipped. I've tried a lot to do this but not successful.

SplashScreen-

public class SplashScreen extends Activity 
{
    /** Called when the activity is first created. */
    TimerTask task;
    SharedPreferences pref;
    SharedPreferences.Editor ed;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.splash_screen);
       pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
       Log.v("","onCreate is calling");
       if(pref.getBoolean("activity_executed", false))
       {
            Log.v("","Before if called");
            setContentView(R.layout.splash_screen);
            Log.v("","after if called");
            new Handler().postDelayed(csRunnable1, 5000);
       } 
       else 
       {
          new Handler().postDelayed(csRunnable2, 5000);  
       }
   }

   Runnable csRunnable1=new Runnable() 
   {       
       @Override
       public void run() 
       {
            Intent intent = new Intent(SplashScreen.this, SplashActivity.class);
               startActivity(intent);
               finish();

       }
   };

   Runnable csRunnable2=new Runnable() 
    {      
       @Override
       public void run() 
       {
            Intent intent = new Intent(SplashScreen.this, LoginActivity.class);
               startActivity(intent);
               finish();

       }
   };
}

SplashActivity-

public class SplashActivity extends Activity implements OnClickListener
{   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        //////////////////////////////////////////////////////////////////////
        //////// GAME MENU  /////////////////////////////////////////////////
        Button playBtn = (Button) findViewById(R.id.playBtn);
        playBtn.setOnClickListener(this);
        Button settingsBtn = (Button) findViewById(R.id.settingsBtn);
        settingsBtn.setOnClickListener(this);
        Button rulesBtn = (Button) findViewById(R.id.rulesBtn);
        rulesBtn.setOnClickListener(this);
        Button exitBtn = (Button) findViewById(R.id.exitBtn);
        exitBtn.setOnClickListener(this);
    }


    /**
     * Listener for game menu
     */
    @Override
    public void onClick(View v) {
        Intent i;

        switch (v.getId()){
        case R.id.playBtn :
            //once logged in, load the main page
            //Log.d("LOGIN", "User has started the game");
            //Get Question set //
            List<Question> questions = getQuestionSetFromDb();
            //Initialise Game with retrieved question set ///
            GamePlay c = new GamePlay();
            c.setQuestions(questions);
            c.setNumRounds(getNumQuestions());
            ((CYKApplication)getApplication()).setCurrentGame(c);  
            //Start Game Now.. //
            i = new Intent(this, QuestionActivity.class);
            startActivityForResult(i, Constants.PLAYBUTTON);
            finish();
            break;

        case R.id.rulesBtn :
            i = new Intent(this, RulesActivity.class);
            startActivityForResult(i, Constants.RULESBUTTON);
            finish();
            break;

        case R.id.settingsBtn :
            i = new Intent(this, SettingsActivity.class);
            startActivityForResult(i, Constants.SETTINGSBUTTON);
            finish();
            break;

        case R.id.exitBtn :
            finish();
            break;
        }

    }

I know this is the line i need to edit in SplashActivity but whenever i add this my app crash.

ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
解决方案

May be you are doing something wrong. you can do simply this in splash screen

   if(pref.getBoolean("activity_executed", false))
   {
        Log.v("","Before if called");
        setContentView(R.layout.splash_screen);
        Log.v("","after if called");
        new Handler().postDelayed(csRunnable1, 5000);
        pref.edit().putBoolean("activity_executed", true).commit();
   } 

or in your splashactivity oncreate call this

getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE).edit().putBoolean("activity_executed", true).commit();

这篇关于如何设置共享preferences其他活动的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 14:16