java - 如何在第一次运行后永远跳过第二个 Activity ?

标签 java android android-intent timer

我正在开发一个包含 SplashScreen.java 我的第一个 Activity 的应用程序。之后显示 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 开始。

最佳答案

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

关于java - 如何在第一次运行后永远跳过第二个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19176518/

相关文章:

java - 将intellij idea连接到sql server数据库

android - Flutter aduioplayers MissingPluginException(未在 channel xyz.luan/audioplayers 上找到方法 startHeadlessService 的实现)

android - 如何从 RTSP 链接播放视频

android - 设备方向上可用的共享 Intent 列表菜单已更改(shareActionProvider)

android - 如何设置 Intent "Intent.EXTRA_ALLOW_MULTIPLE"的最大文件选择计数。用户最多可以选择一定的限制?

java - 使用 javaservice.exe File.exists() 返回 false

java - 使用 AngularJs 和 Java 在 SQL 中存储图像?

java - 需要创建 Java JAR 文件并将其添加到 Maven Dependency

java - 安卓 Intent 服务 : Unable to start

android - onReceive() android.net.ConnectivityManager.CONNECTIVITY_ACTION