2

我在 ICS 4.0.3 上运行线程时遇到问题

我制作了 apk,并将其安装在 2.33 SE 手机和 Google Nexus S 4.0.3 上。

在 SE 上,apk 正在按预期运行和加载。但是,在 Nexus 上出现错误,我必须关闭它。但我看到程序在后面运行。我试图从代码中删除线程(加载屏幕,启动画面)并在 Nexus 上再次运行它,它可以运行。所以我发现我的问题出在线程上,onCreate 线程正在启​​动。它从 2.33 到 4.0.3 有什么区别吗?

package my.LigTv.Browser;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.webkit.WebView;
import android.app.AlertDialog;
public class LigTVBrowserActivity extends Activity {
/** Called when the activity is first created. */


WebView mWebView;
AlertDialog alertDialog;
protected boolean _active = true;
protected int _splashTime = 3000; // time to display the splash screen in ms
int progress = 0;


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



    final Activity activity = this;

    final ProgressDialog progressDialog = new ProgressDialog(activity);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setMessage("Please wait for the applet to load");

    progressDialog.show();
    progressDialog.setProgress(0);
    activity.setProgress(progress * 1000);

    progressDialog.incrementProgressBy(progress);

    if(progress == 100 && progressDialog.isShowing())
        progressDialog.dismiss(); 



    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 {
                finish();
                startActivity(new Intent("my.LigTv.Browser.Starter"));
                stop();
            }
        }
    };
    splashTread.start();
}    

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

 }
4

3 回答 3

1

finish()开始新活动后尝试调用。另外,你为什么stop()在线程上调用?一旦它开始了新的活动,它就会停止。这是一个我已经写过的例子,你可以随意改变:

            Runnable r1 = new Runnable(){
                public void run(){
                    new Handler().postDelayed(new Runnable(){
                        @Override
                        public void run(){
                            startActivity(new Intent(Splash.this,Main.class));
                            overridePendingTransition(R.anim.fade_out, R.anim.fade_in);
                            finish();

                        }
                    }, waitTime);
                }
            };
            runOnUiThread(r1);
于 2012-04-24T11:33:47.197 回答
1

我遇到了同样的问题,我用另一个代码作为快速修复来修复它:

private Timer myTimer;
private int x=0;
private int interval=2500;

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

    myTimer = new Timer();
    myTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            TimerMethod();
        }

    }, 0, interval);

}

private void TimerMethod()
{
    this.runOnUiThread(Timer_Tick);
}

private Runnable Timer_Tick = new Runnable() {
public void run() {
    if(x==1)
    {
        SplashActivity.this.finish();
        startActivity(new Intent(SplashActivity.this,LoginActivity.class));
    }
    x++;
}
};

但是没有进度条,

如果您想在用户单击启动屏幕时执行此操作,他会转到下一个活动,您可以通过取消计时器并完成启动活动并转到另一个来做到这一点。

于 2012-04-24T12:01:37.130 回答
0

这不应该在ui线程中吗?

     runOnUiThread(new Runnable(){
                finish();
                startActivity(new Intent("my.LigTv.Browser.Starter"));
                stop();
});
于 2012-04-24T11:32:20.270 回答