我的应用有一个启动活动
必须至少显示5秒
但是在这个活动中,我有另一个线程来同步来自互联网的数据
同步过程可能需要5秒以上或不到5秒。
如果少于5秒,处理程序应该等到第五秒
如果超过5秒,处理程序应该等到进程完成
如何让Handler等待另一个线程?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
syncFromInternet(); // another thread may over 5 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent;
intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
}, 5000);
}
答案 0 :(得分:1)
您可以使用@Override
protected void onCreate(Bundle savedInstanceState) {
...
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
private static final long FIVE_SECONDS = 5 * 1000;
private volatile Date mStartTime;
@Override
protected void onPreExecute() {
mStartTime = new Date();
}
@Override
protected Void doInBackground(Void... params) {
// Do the syncing here
syncFromInternet();
Date now = new Date();
long execTime = now.getTime() - mStartTime.getTime();
if(execTime < FIVE_SECONDS) {
Thread.sleep(FIVE_SECONDS - execTime);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Intent intent = new Intent(...);
startActivity(intent);
finish();
}
};
asyncTask.execute(null, null);
}
实例并测量与远程服务器同步的时间。如果时间大于5分钟,则开始新活动,否则 - 等到第5秒:
{{1}}
答案 1 :(得分:1)
您可以使用此代码:
package com.example.untitled;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.File;
import java.io.IOException;
public class MyActivity extends Activity {
private volatile boolean isAvailable = false;
private volatile boolean isOver = false;
private Handler messageHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
if (isOver) {
Log.e("messageHandler","isOver");
transitToNewActivity();
}else {
Log.e("messageHandler","isOver false");
}
break;
}
}
};
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
syncFromInternet(); // another thread may over 5 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
isOver = true;
Log.e("mainHandler", "Main handler expired");
if (isAvailable) {
transitToNewActivity();
Log.e("mainHandler", "isAvailable");
}else {
Log.e("mainHandler","isAvailable false");
}
}
}, 50000);
public void transitToNewActivity() {
Log.e("transitToNewActivity","Activity transited");
Intent intent;
intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
public void syncFromInternet() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(80000);
isAvailable = true;
messageHandler.sendEmptyMessage(0);
Log.e("syncFromInternet", "internet data synced");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
请根据您的要求进行修改。
答案 2 :(得分:1)
你必须制作简单的逻辑,如
当处理程序完成时,检查syncFromInternet方法是否完成,如果它已完成打开主要活动
当syncFromInternet完成时,检查处理程序是否完成,如果它已完成打开主要活动。
代码中的上述说明:
boolean isHandlerCompleted = false, isAsyncCompleted = false;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
isHandlerCompleted = true;
if(isHandlerCompleted && isAsyncCompleted)
{
openMainActivity();// both thread completed
}
}
}, 5000);
// in your async task add this condition when it completes its task
isAsyncCompleted = true;
if(isHandlerCompleted && isAsyncCompleted)
{
openMainActivity();// both thread completed
} // till this line
// make this function to open main activity
openMainActivity()
{
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
答案 3 :(得分:0)
执行线程后在syncFromInternet()
内调用您的处理程序。
答案 4 :(得分:0)
您应该实施AsyncTask
,以便在syncFromInternet()
完成之前等待。
private class BackgroundSplashTask extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
// show progressbar
}
@Override
protected Object doInBackground(Object[] params) {
try {
syncFromInternet(); // sync in background
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
// dismiss progressbar
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent); // go to SecondActivity after syncFromInternet is completed
finish();
}
}