我一直在搜索整個互聯網,以解決這個問題在我的應用程序,但沒有什麼真的似乎爲我工作。安卓替代睡眠後啓動Intent方法
我有一個起始頁面,它處理我的Json-Data的下載,3秒鐘後,一個Intent啓動並將我重定向到應用程序。這工作正常,如果我有更快的互聯網連接。如果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;
}
可以使用處理器 – Raghunandan
ü可以specificate你的答案嗎? =) –