1

我正在开发一个包含SplashScreen.java我的第一个活动的应用程序。之后显示LoginActivity.java登录。而我的LoginActivity.java课终于开始了SplashActivity.java。每次我启动我的应用程序SplashScreen.java调用SplashActivity.java而不是LoginActivity.java. 为此,我在我的SplashScreen.java课堂上进行了一些更改,但效果不佳。

SplashScreen.java 类-

public class SplashScreen extends Activity 
{
    private long splashDelay = 5000; //5 seconds
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
        if(pref.getBoolean("activity_executed", false))
        {
            Intent intent = new Intent(this, SplashActivity.class);
            startActivity(intent);
            finish();
        } 
        else 
        {
            Editor ed = pref.edit();
            ed.putBoolean("activity_executed", true);
            ed.commit();
        }
        TimerTask task = new TimerTask()
        {

            @Override
            public void run() {
                finish();
                Intent mainIntent = new Intent().setClass(SplashScreen.this, LoginActivity.class);
                startActivity(mainIntent);
            }

        };

        Timer timer = new Timer();
        timer.schedule(task, splashDelay);
    }
}

其他人帮助我。现在第一次运行后唯一的问题。应用程序从开始SplashActivity而不是从SplashScreen.

4

7 回答 7

2

第 1 步:在你的登录类中声明这个

SharedPreferences pref;
SharedPreferences.Editor editor;

第二步:在 onCrete 方法中声明

pref = getSharedPreferences("login", MODE_PRIVATE);
editor = pref.edit();

第 3 步:登录页面中的提交按钮的 OnClick 粘贴以下行:

String check=pref.getString("selected", "nil")
if(check.equals("TRUE"))
{
Intent intent=new Intent(this,splash.class);
startActivity(intent);
}

第 4 步:当用户凭据和数据库凭据匹配时,将其放在您获得成功消息的位置。

editor.putString("selected", "TRUE");
editor.commit();
于 2013-10-05T09:38:02.057 回答
2
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash_screen);
            SharedPreferences pref = getSharedPreferences("ActivityPREF",  Context.MODE_PRIVATE);
            Editor ed = pref.edit();
     Boolean Exist=pref.getBoolean("activity_executed", false);// Check is user logged in or not
            if(Exist)// if allready logged in then forward it to Splash Activity
            {

                Intent intent = new Intent(this, SplashActivity.class);
                startActivity(intent);
                finish();
            } 
            else // Not logged in
            {

               Handler handler = new Handler();
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable, 2000L);
             }
Runnable runnable = new Runnable() {
        public void run() {

            new AsyncParsing().execute();

        }
    };

    private class AsyncParsing extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            Log.d("Splash Activity", "In pre execute");

        }

        @Override
        protected Void doInBackground(Void... params) {
            Log.d("Splash Activity", "In do in backgriund ");


            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.d("Splash Activity", "In post execute");
                   Intent intent = new Intent(SplashScreen.this, LoginActivity.class);
                startActivity(intent);
                finish();
                }
           }
    }
于 2013-10-04T10:26:28.163 回答
1

@John 检查并仅参考此工作代码,我希望它对您有所帮助,

    setContentView(R.layout.main);
    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.menu_frame);
         Log.v("","after if called");
         new Handler().postDelayed(csRunnable1, 3000);

    } 
    else 
    {
       new Handler().postDelayed(csRunnable2, 3000);  
        Editor ed = pref.edit();
        ed.putBoolean("activity_executed", true);
        ed.commit();

    }


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

    }
};
于 2013-10-04T09:20:42.473 回答
0

我认为你有一个逻辑问题。应该如果没有执行,运行它并标记为已执行。

if(!pref.getBoolean("activity_executed", false)) {
    Editor ed = pref.edit();
    ed.putBoolean("activity_executed", true);
    ed.commit();
    Intent intent = new Intent(this, SplashActivity.class);
    startActivity(intent);
    finish();
}
于 2013-10-04T08:37:20.340 回答
0

问题是调用完成并不能阻止 onCreate 的其余部分被执行。你需要在后面加上一个return语句:

Intent intent = new Intent(this, SplashActivity.class);
startActivity(intent);
finish();
于 2013-10-04T08:41:12.120 回答
0
// try this
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
        if(pref.getBoolean("activity_executed", false))
        {
            Intent intent = new Intent(this, SplashActivity.class);
            startActivity(intent);
            finish();
        }
        else
        {
            Editor ed = pref.edit();
            ed.putBoolean("activity_executed", true);
            ed.commit();
            Timer timer = new Timer();
            TimerTask task = new TimerTask()
            {

                @Override
                public void run() { 
                    timer.cancel();
                    Intent mainIntent = new Intent().setClass(SplashScreen.this, LoginActivity.class);
                    startActivity(mainIntent);
                    finish();
                }

            };

            timer.schedule(task, splashDelay);
        }

    }
于 2013-10-04T08:41:24.527 回答
0

你想念我希望它能解决你的问题

public class SplashScreen extends Activity 
{private long splashDelay = 5000; // 5 seconds

    /** Called when the activity is first created. */
    TimerTask task = new TimerTask() {

        @Override
        public void run() {
            finish();
            Intent mainIntent = new Intent().setClass(Test.this,
                    LoginActivity.class);
            startActivity(mainIntent);
        }

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        SharedPreferences pref = getSharedPreferences("ActivityPREF",
                Context.MODE_PRIVATE);

        if (pref.getBoolean("activity_executed", false)) {
            Intent intent = new Intent(this, SplashActivity.class);
            startActivity(intent);
            finish();
        } else {

            Editor ed = pref.edit();
            ed.putBoolean("activity_executed", true);
            ed.commit();



            Timer timer = new Timer();
            timer.schedule(task, splashDelay);




        }

    }
}
于 2013-10-04T08:43:18.360 回答