Android替代方法在sleep-Method之后启动Intent

时间:2013-12-21 17:28:20

标签: android json multithreading android-intent

我一直在互联网上搜索,在我的应用程序上解决这个问题,但似乎没有什么对我有用。

我有一个起始页面,它处理我的Json-Data的下载,3秒后,Intent启动并重定向到App。如果我有更快的Internet连接,这工作正常。如果因特网连接在3秒内加载所有数据的速度很慢,则会抛出异常。

所以我的问题是,如何获得下载过程的实时时间?

我应该用2个线程来处理吗?

谢谢! =)

代码如下:

public class StartLogoPage extends Activity {

boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
private Thread timer;
private Thread intentThread;
private Thread timerNoInternet;
private boolean serverResponse = false;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_startlogopage);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    // startIntent();
    haveNetworkConnection();
    Dashboard ds = new Dashboard();

    if (haveConnectedWifi == true || haveConnectedMobile == true) {

        Toast.makeText(this, "Loading data from Internet",
                Toast.LENGTH_LONG).show();

        timer = new Thread() {
            public void run() {

                try {
                    System.out.println("timer Thread started.");
                    // start timer
                    UrlHandler urlHandler = new UrlHandler();
                    urlHandler.handleEvents(1);
                    urlHandler.handleNews(1);
                    urlHandler.handlePerson(1);
                    urlHandler.handleWebIndex(1);
                    urlHandler.handleNavigation(1);

                    sleep(3000);

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                    Intent openDashboard = new Intent(
                            "com.example.incomingandroid.DASHBOARD");
                    System.out.println("openDashboard" + openDashboard);
                    // openDashboard.clone();
                    // if (openDashboard != null)
                    startActivity(openDashboard);

                }

            }

        };
        timer.start();



    } else {

        Toast.makeText(
                this,
                "No Internet Connection was found. This Application needs Internet to be started.",
                Toast.LENGTH_LONG).show();

        timerNoInternet = new Thread() {
            public void run() {
                try {
                    sleep(6000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    finish();
                }
            }
        };
        timerNoInternet.start();
    }

}

private boolean haveNetworkConnection() {

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;

        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;

    }
    return haveConnectedWifi || haveConnectedMobile;
}

1 个答案:

答案 0 :(得分:0)

您可以使用Handler

 Handler h = new Handler();

    Runnable r = new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub

        }

    };
    h.postDelayed(r, 30000);//delay 3 seconds

如果您想在延迟3秒后想要某事,可以使用Handler

编辑:

您可以使用AsyncTask在那里下载,然后在onPostExecute中启动活动。在onPostExecute计算完成后执行doInbackground

http://developer.android.com/reference/android/os/AsyncTask.html