Splash Image for android

↘锁芯ラ 提交于 2019-12-10 11:32:45

问题


I made a splash image to show at the start of my activity.. The image show perfectly.But the problem is when i call this

public class SplashImageActivity extends Activity {
    protected boolean active = true;
    protected int splashTime = 5000; // time to display the splash screen in ms

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        // thread for displaying the SplashScreen
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(active && (waited < splashTime)) {
                        sleep(100);
                        if(active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    startActivity(new Intent(SplashImageActivity.this,Myapps.class));
                    finish();
                    //startActivity(new Intent("com.splash.com.MyApps"));
                    //startActivity( new Intent(getApplicationContext(), Myapps.class));
                }
            }
        };
        splashTread.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            active = false;
        }
    return true;
    }
}

go for next activity the stop() does not work. And it does not go to this activity. I add all activity in manifest. The stop() shows in code like this

what's the problem?


回答1:


No need to call stop() and call finish() after starting activity

finally 
{

    startActivity(new Intent(currentclass.this,nextActivity.class);
    finish();
}



回答2:


I use thread to show the Splash screen, and it works for me:

       @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    mSplashThread =  new Thread(){
        @Override
        public void run(){
            try {
                synchronized(this){
                    wait(4000);
                }
            }catch(InterruptedException ex){                   
            }
            finish();

            Intent i=new Intent(getApplicationContext(),NextActivity.class);
            startActivity(i);

            interrupt();
        }

    }; 
    mSplashThread.start();        
}



回答3:


Please try below code..

public class Splashscreen extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Thread t2 = new Thread() {
                public void run() {
                    try {
                        sleep(2000);
                        startActivity( new Intent(getApplicationContext(), Exercise.class));
                        finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            t2.start();
        }
    }



回答4:


No need to call stop() just call finish() after starting activity

finally {
startActivity(new Intent(currentclass.this,nextActivity.class);
finish();
}

You can also use handler an postdelayed() to make a splash screen like below

 public class SplashScreenActivity extends Activity{

        private Handler handler;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash_screen);

            final Runnable runnable = new Runnable() {

                @Override
                public void run() {
                       Intent intent=new Intent(SplashScreenActivity.this, nextActivity.class);
                       startActivity(intent);
                       finish();

                }
            };
            handler = new Handler();
            handler.postDelayed(runnable, 5000);


        }

    }

You will show your splash screen for 5 seconds and then move to next Activity




回答5:


first thing it is not onStop of Activity so looks you are calling stop function of thread which is Deprecated that's why you are getting the strike line so use other way to stop the thread of use better way to implement the splash ........

as looks you try some thing like this link



来源:https://stackoverflow.com/questions/11183828/splash-image-for-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!