线程停止发行安卓 [英] Thread stopping issue android
问题描述
我要停止正在运行的线程同时点击启动屏幕上,如果我没有在屏幕上点击,线程执行后,将启动另一个活动。但是,让 UnSupportedException
,我该如何解决呢?
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_splash);
IIMAGE =(ImageView的)findViewById(R.id.iIcon);
的splashimage =(ImageView的)findViewById(R.id.splash_image); iImage.setOnClickListener(本);
splashImage.setOnClickListener(本);
}@覆盖
保护无效调用onStart(){
super.onStart();
splashTimer =新主题(新的Runnable(){
@覆盖
公共无效的run(){
尝试{
视频下载(5000);
}赶上(InterruptedException的E){
e.printStackTrace();
}
startActivity(新意图(SplashActivity.this,LoginAuthenticationActivity.class));
完();
}
});
splashTimer.start();
}@覆盖
公共无效的onClick(查看视图){
如果(splashTimer.isAlive())
splashTimer.stop();
开关(view.getId()){
案例R.id.iIcon:
startActivity(新意图(这一点,AboutUsActivity.class));
打破;
案例R.id.splash_image:
startActivity(新意图(这一点,LoginAuthenticationActivity.class));
打破;
默认:
打破;
}
完();
}
日志:
三月一日至27日:27:01.189:W / dalvikvm(1080):主题ID = 1:螺纹未捕获的异常(组= 0x409c01f8)退出
3月1日至27日:27:01.209:E / AndroidRuntime(1080):致命异常:主要
3月1日至27日:27:01.209:E / AndroidRuntime(1080):java.lang.UnsupportedOperationException
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在java.lang.Thread.stop(Thread.java:1076)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在java.lang.Thread.stop(Thread.java:1063)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在com.app.wooqer.SplashActivity.onClick(SplashActivity.java:48)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在android.view.View.performClick(View.java:3511)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在android.view.View $ PerformClick.run(View.java:14105)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在android.os.Handler.handleCallback(Handler.java:605)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在android.os.Handler.dispatchMessage(Handler.java:92)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在android.os.Looper.loop(Looper.java:137)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在android.app.ActivityThread.main(ActivityThread.java:4424)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在java.lang.reflect.Method.invokeNative(本机方法)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在java.lang.reflect.Method.invoke(Method.java:511)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
3月1日至27日:27:01.209:E / AndroidRuntime(1080):在dalvik.system.NativeStart.main(本机方法)
你在做什么是很浪费的(任何闪屏是一种浪费,但使用线程这样的更是如此),但解决您的问题:
使用 interrrupt();
这一翻译停止()的;
由于文档说的停止()
抛出UnsupportedOperationException。
和解决重复的问题,移动 startActivity()
中的尝试
,所以它看起来是这样的:
公共无效的run(){
尝试{
视频下载(5000);
startActivity(新意图(SplashActivity.this,LoginAuthenticationActivity.class));
}赶上(InterruptedException的E){
e.printStackTrace();
}
完();
}
这样,当你调用中断()
所有的活动确实是完成()
和重复的 startActivity()
不叫。
要进一步解释:
第一个问题:停止()
抛出默认情况下例外,因为它是这你不应该使用不安全的方法。
然后中断()
,你有 startActivity()
中的run方法后 catch块。当你打断, startActivity()
在调用一次的run()
和一度的onClick ()
。通过移动 startActivity()
中的尝试
块到右后视频下载()
,当中断()
中断发
的不执行试
块。这意味着,你现在只能有一个 startActivity()
通话,而不是两个。欲了解更多信息,请在例外的阅读起来。
I want to stop a running thread while click on the splash screen, if I don't click on screen, after the thread execution, it will launch another Activity. But getting UnSupportedException
, how do I solve it?
@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();
}
Log:
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)
What you're doing is very wasteful (any splash screen is wasteful, but using Threads like this is more so), but to fix your issue:
use interrrupt();
intead of stop();
As the docs say for stop()
Throws UnsupportedOperationException.
And to fix the duplicate issue, move the startActivity()
inside the try
so it looks like this:
public void run() {
try {
Thread.sleep(5000);
startActivity(new Intent(SplashActivity.this, LoginAuthenticationActivity.class));
} catch (InterruptedException e) {
e.printStackTrace();
}
finish();
}
That way when you call interrupt()
all your Activity does is finish()
and the duplicate startActivity()
is not called.
To further explain:
Very first issue: stop()
throws an exception by default, since it's an unsafe method which you're not supposed to use.
Then when you used interrupt()
, you had startActivity()
in the run method after the catch block. When you interrupted, startActivity()
was called once in run()
and once in onClick()
. By moving startActivity()
inside the try
block to right after Thread.sleep()
, when interrupt()
interrupts the Thread
, the rest of the try
block isn't executed. This means that you now only have one startActivity()
call instead of two. For more information, read up on exceptions.
这篇关于线程停止发行安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!