线程停止问题android

时间:2013-01-26 21:47:13

标签: java android multithreading splash-screen

我想在点击启动画面时停止正在运行的线程,如果我没有点击屏幕,在线程执行后,它将启动另一个Activity。但是获得UnSupportedException,我该如何解决?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    iImage = (ImageView) findViewById(R.id.iIcon);
    splashImage = (ImageView) findViewById(R.id.splash_image);

    iImage.setOnClickListener(this);
    splashImage.setOnClickListener(this);
}

@Override
protected void onStart() {
    super.onStart();
    splashTimer = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            startActivity(new Intent(SplashActivity.this, LoginAuthenticationActivity.class));
            finish();
        }
    });
    splashTimer.start();
}

@Override
public void onClick(View view) {
    if(splashTimer.isAlive())
        splashTimer.stop();
    switch (view.getId()) {
        case R.id.iIcon:
            startActivity(new Intent(this, AboutUsActivity.class));
            break;
        case R.id.splash_image:
            startActivity(new Intent(this, LoginAuthenticationActivity.class));
            break;
        default:
            break;
    }
    finish();
}

日志:

01-27 03:27:01.189: W/dalvikvm(1080): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-27 03:27:01.209: E/AndroidRuntime(1080): FATAL EXCEPTION: main
01-27 03:27:01.209: E/AndroidRuntime(1080): java.lang.UnsupportedOperationException
01-27 03:27:01.209: E/AndroidRuntime(1080):     at java.lang.Thread.stop(Thread.java:1076)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at java.lang.Thread.stop(Thread.java:1063)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at com.app.wooqer.SplashActivity.onClick(SplashActivity.java:48)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at android.view.View.performClick(View.java:3511)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at android.view.View$PerformClick.run(View.java:14105)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at android.os.Handler.handleCallback(Handler.java:605)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at android.os.Looper.loop(Looper.java:137)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at android.app.ActivityThread.main(ActivityThread.java:4424)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at java.lang.reflect.Method.invokeNative(Native Method)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at java.lang.reflect.Method.invoke(Method.java:511)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-27 03:27:01.209: E/AndroidRuntime(1080):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

你正在做的事情是非常浪费的(任何启动画面都是浪费,但使用像这样的线程更是如此),但要解决你的问题:

使用interrrupt();

stop(); intead

docs代表stop()

Throws UnsupportedOperationException.

要解决重复问题,请将startActivity()移到try内,使其如下所示:

public void run() {
  try {
    Thread.sleep(5000);
    startActivity(new Intent(SplashActivity.this, LoginAuthenticationActivity.class));
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  finish();
}

这样,当您致电interrupt()时,您的所有活动都会finish(),并且不会调用重复的startActivity()

进一步解释:

非常第一个问题:默认情况下stop()会抛出异常,因为它是一个不安全的方法,你不应该使用它。

然后,当您使用interrupt()时,在后面的运行方法中有startActivity()。当您被中断时,startActivity()中的run()被召唤一次onClick()被召唤一次startActivity()。通过将try块内的Thread.sleep()移至interrupt()之后,当Thread中断try时,startActivity()块的其余部分将为'执行。这意味着您现在只有一个{{1}}来电,而不是两个。有关详细信息,请阅读exceptions