【问题标题】:Thread within a Splash Screen启动画面中的线程
【发布时间】:2013-02-07 21:04:10
【问题描述】:
package com.example.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class Splash extends Activity {
    private Intent myintent;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        myintent = new Intent(this, MainActivity.class);
        splashScreen(1000); }

    public void splashScreen (final int x)
    {
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(x);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                startActivity(myintent);
                finish();
            }
        }).run();
    }

}

有代码,问题出在:SplashScreen 没有获取启动 XML 布局文件的内容视图...现在,我怀疑这是一个线程问题,并且之前以某种方式执行了线程setContentView 方法虽然该方法位于代码中线程的运行方法之前,所以我这样想是不合逻辑的,但我觉得这个启动画面不起作用的原因已经用完了

【问题讨论】:

  • 通过使用run(),您仍然会阻塞 UI 线程。请改用.start()。还要记住,使用线程对于启动屏幕来说并不是那么好,您也可以尝试使用处理程序。但首先要评估是否需要喷溅。
  • 非常感谢! :) 你能告诉我运行和启动的区别吗
  • 调用run()是直接方法调用。使用.start() 实际上是在run() 中的执行在单独的线程中运行。
  • 再次非常感谢 A--C 非常感谢!!! :)

标签: java android multithreading thread-sleep


【解决方案1】:

thread.run() 更改为thread.start()http://www.javafaq.nu/java-article1131.html

new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(x);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            startActivity(myintent);
            finish();
        }
    }).start();

实现 Splash 的更好方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    myintent = new Intent(this, MainActivity.class);

   new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
            startActivity(myintent);
            finish();
        }
    }, 1000);
}

【讨论】:

  • 是的,这是真的谢谢你和 A--C 已经告诉我了,但我会选择你的答案,因为他没有把它放在答案中,而是在评论中再次感谢你来回答
【解决方案2】:
    package com.echo.myatlsnookpaid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

public class Splash extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splash);

        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(3500);
//                  sleep(100);
                }
                catch(InterruptedException e){
                    e.printStackTrace();
                } finally {
                    Intent openMain = new Intent(Splash.this, MainActivity.class);
                    startActivity(openMain);
                    finish();
                }
            }
        };
        timer.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}

在你的清单中,给

<intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

过滤到他的splashactivity。

【讨论】:

  • 感谢清单已经是这样了,是的,我需要做的就是用 start 替换 run 方法
  • 啊。我一开始就误读了您的示例代码。是的,你需要改变的只是开始。
猜你喜欢
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多