2

是否可以执行将执行 HTTP 请求的启动画面,如果此请求执行时间过长,即 7-10 秒,则中止请求并跳转到主要活动?

下面的代码是我所做的,但它不起作用 - 超时不起作用,HTTP 请求和跳转正在起作用。据我了解,可以延迟使用AsyncTask'get()方法或处理程序。Get()方法应该在单独的线程中,但它不起作用。这个任务怎么做?

编辑:

public class SplashActivity extends Activity {
private static final String TAG = "SplashActivity";
private Handler handler = new Handler();
private Runnable r;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_layout);

    if (Helpers.isNetworkConnected(getApplicationContext())) {
        Log.d(TAG, "Has Internet");
        final DownloadFAQ downloadFAQ = new DownloadFAQ();
        new Thread(new Runnable() {
            public void run() {
                try {
                    Log.d(TAG, "Timing...");
                    downloadFAQ.execute().get(1000, TimeUnit.MILLISECONDS);

                    SplashActivity.this.runOnUiThread(new Runnable() {
                        public void run() {
                            Log.d(TAG, "redirect");
                            redirect();
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    downloadFAQ.cancel(true);
                    Log.d(TAG, "Task has benn canceled");
                    if (downloadFAQ.isCancelled())
                        redirect();
                }
            }
        }).start();
    } else {

        r = new Runnable() {
            public void run() {
                redirect();
            }
        };
        handler.postDelayed(r, 2500);
    }
}

private class DownloadFAQ extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        Log.d(TAG, "Execute task");
        ServerAPI server = new ServerAPI(getApplicationContext());
        server.serverRequest(ServerAPI.GET_FAQ, null);
        return null;
    }
}

private void redirect() {
    Intent i = new Intent(SplashActivity.this, TabsActivity.class);
    startActivity(i);
    finish();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    handler.removeCallbacks(r);
}

}

4

2 回答 2

5

因为您正在尝试在 AsyncTaskdoInBackground仍在运行时再次启动它。更改您的代码以使其正常工作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_layout);
    downloadFAQ = new DownloadFAQ();
    new Thread(new Runnable() {
            public void run() {
                try {
                    downloadFAQ.execute().get(2000, TimeUnit.MILLISECONDS);

                  SplashActivity.thisrunOnUiThread(new Runnable() {
                        public void run() {
                         // start Activity here
                          Intent i = new Intent(SplashActivity.this,
                                                      TabsActivity.class);
                          SplashActivity.this.startActivity(i);
                          SplashActivity.this.finish();
                       }
                  });
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
}

并且您需要downloadFAQ.get(2000, TimeUnit.MILLISECONDS);从 doInBackground 方法中删除将您的 AsyncTask 更改为

private class DownloadFAQ extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        ServerAPI server = new ServerAPI(getApplicationContext());
        server.serverRequest(ServerAPI.GET_FAQ, null);
        return null;
    }

    protected void onPostExecute(Void result) {

    }

}
于 2013-01-31T10:34:47.453 回答
0

考虑使用 asyncTask 状态: AsyncTask.Status

于 2013-01-31T10:20:29.273 回答