Android 不支持不推荐的线程方法

Android 不支持不推荐的线程方法,android,Android,我正在做一个项目,我需要显示主页,当主页显示,之后或继续与3至5秒,我的另一个欢迎自定义对话框显示。但这样做,会出现以下错误,但我的应用程序不会停止工作。。LogCat显示这些错误。 申请代码: final Dialog d=new Dialog(Main.this); d.setContentView(R.layout.SplashScreen); Thread splashTread = new Thread() { @Override p

我正在做一个项目,我需要显示主页,当主页显示,之后或继续与3至5秒,我的另一个欢迎自定义对话框显示。但这样做,会出现以下错误,但我的应用程序不会停止工作。。LogCat显示这些错误。 申请代码:

  final Dialog d=new Dialog(Main.this);
    d.setContentView(R.layout.SplashScreen);
    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                d.show();

                int waited = 0;
                while(_active && (waited < _splashTime)) {
                    sleep(100);
                    if(_active) {
                        waited += 100;
                    }
                }
            } catch(InterruptedException e) {
                // do nothing
            } finally {
                d.cancel();
                    stop();
            }
        }
    };
    splashTread.start();
}

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

在Android中,最好使用
处理程序
来管理
线程
可运行文件

创建一个处理程序实例

Handler handler = new Handler();
创建一个可运行的线程

Runnable runnable = new Runnable() {

        @Override
        public void run() {
            Log.d("runnable started", "inside run");
            handler.removeCallbacks(runnable);
            handler.postDelayed(runnable, 1000);
        }
    };
并使用处理程序启动Runnable

handler.postDelayed(runnable, 1000);
并停止可运行的使用

handler.removeCallbacks(runnable);

此链接准确地告诉您问题是什么,以及如何解决问题:

Thread.stop是一个不推荐使用的API,而不推荐使用的线程方法则不是 Android支持。因此,它抛出了一个 不支持操作异常

答案是不使用Thread.stop-在 更优雅的方式,例如通过设置一个标记 定期检查

此链接讨论了为什么thread.stop()被弃用(很久以前在Java中就被弃用了,而不仅仅是在Android中!)


将此最佳方式用于启动屏幕

int SPLASH_TIME = 1300;
    Handler HANDLER = new Handler();

                    // thread for displaying the SplashScreen
                    HANDLER.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                         finish();
                         startActivity (new Intent(getApplicationContext(),Alpha.class));
                        }
                      }, SPLASH_TIME);

我从外侧使用handler。。当放入run()方法时,它会显示runnable is not initialized error。您必须将顶部的处理程序声明为全局处理程序并使用它。请注意,此方法可能会在某些android设备中导致链接问题。
int SPLASH_TIME = 1300;
    Handler HANDLER = new Handler();

                    // thread for displaying the SplashScreen
                    HANDLER.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                         finish();
                         startActivity (new Intent(getApplicationContext(),Alpha.class));
                        }
                      }, SPLASH_TIME);