android - Ice Cream Sandwich 上的线程

标签 android multithreading loading

我在 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;
}

 }

最佳答案

尝试在启动新 Activity 后调用finish()。另外,为什么要在线程上调用 stop() ?一旦开始新的 Activity ,它将停止。这是我已经写好的示例,您可以随意更改:

            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);

关于android - Ice Cream Sandwich 上的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10296914/

相关文章:

java - 如何从 LinkedHashSet 中获取前 5 个值?

JAVA:经过这么多时间后中断线程

java - 使用线程池将紧耦合方法转换为松耦合的优雅方法

java - 什么时候应该优先选择 Callable 而不是 Runnable?为什么?

jqGrid 触发 "Loading..."叠加

android - AppBarLayout setExpanded速度慢

java - 在由按钮触发的主 Activity 中创建不同的 Activity

java - Android RecyclerView 滚动并将项目置于顶部

jquery - Chrome 中的颜色框问题

javascript - 为什么这个 jquery 代码在刷新页面后不起作用?