在我的Activity类中实现了UINotifier,用于在调用TimerTask对象时通知Activity类。在我的doInBackground()中,我只调用位于ASyncTask类(connectTask)中的函数。执行onPostExecute,最后,我调用位于Activity类中的方法“ReConnect()”。 代码及其流程是:
// OF Interface UINotifier
@Override
public void notifyUI() {
Log.i(TAG, "GOT MESSAGE FROM MonitorConnection");
monitorTimer.cancel();
Log.i(TAG, "Preparing to Start");
PrepareToStartToConnect();
//publishProgress(4);
int status = 1;
if (status == 1 || status == 2)
ReConnect();
}
// Called on "Connect" button
private void ConnectClicked() {
Log.i(TAG, "Inside Connectclicked");
if (isConnectEligeble()) {
Log.i(TAG, "isConnectionEligible = true");
currentState= CONNECTING;
connectTask = new ConnectTask();
Log.i(TAG, "CReated ins of ConnectTask");
connectTask.execute("");
}
}
private void ReConnect() {
connectTask = null;
if (HttpUtilities.checkInternetConnection()) {
Log.i(TAG, "ABOUT TO RE-CONNECT");
ConnectClicked();
} else {
Log.i(TAG, "No Interne Mesage");
mMessage.setText(R.string.NO_INERNET);
}
return;
}
private void PrepareToStartToConnect() {
monitorTimer = null;
monitorConn = null;
// Make all vars, objects to null
}
// Start TimerTask & Timer object
private void StartTimer() {
Log.i(TAG, "Into Start Timer");
monitorConn = new MonitorConnection(this); // new MonitorConnection(connectTask);
monitorTimer = new Timer();
monitorTimer.schedule(monitorConn, 0, 30000);
}
/**
* ConnectTask AsyncTask class that handles all activities to get connected to the server
*/
private class ConnectTask extends AsyncTask<String, Integer, Boolean> implements UINotifier {
private ProgressDialog dialog = null;
private UServer us = null;
private VPNServer vs = null;
private ConfigData cd = null;
String serverHost = "", errorMsg = "";
private int status = -1; // 0 Connected 1 = ReConnect 2 = Failed to Connect
public ConnectTask() {
dialog = new ProgressDialog(StartUltimate.this);
us = servers.get(selectedRowIndex);
vs = us.getVpnServer(selectedProtocol, selectedPort);
serverHost = us.getServerHost();
}
private void disposeAll() {
// Dispose all objs
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Log.i(TAG, "Into onPostExecute()");
if (result == false)
mMessage.setText(errorMsg);
else
mMessage.setText("");
dialog.dismiss();
// Clean up all variables of the object
disposeAll();
Log.i(TAG, "FINISHED onPostExecute()");
// Go out of AsyncTask and have control to other method of Activity class
StartTimer();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
selectedServerHost = serverHost.substring(0, serverHost.indexOf('.'));
dialog.setCancelable(false);
dialog.setIcon(R.drawable.icon);
dialog.setTitle("Progessing...");
dialog.setMessage(String.valueOf(R.string.ui_activity_authenticating));
dialog.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Set all updates to UI
return;
}
@Override
protected Boolean doInBackground(String... params) {
Log.i(TAG, "Into doBackground...");
......
Log.i(TAG, "Starting Connect.....");
StartConnect();
return true;
}
@Override
public void notifyUI() {
Log.i(TAG, "GOT MESSAGE FROM MonitorConnection");
monitorTimer.cancel();
Log.i(TAG, "Preparing to Start");
PrepareToStartToConnect();
publishProgress(4);
status = 1;
if (status == 1 || status == 2)
ReConnect();
}
private void StartConnect() {
// This is empty right now
}
} // END OF ConnectTask class
调用StartTimer,MonitorThread通知Activity类,并调用ReConnect调用ConnectClicked。在ConnectClicked中, connectTask = new ConnectTask();是我得到此异常和日志的地方:
04-15 17:09:07.501: INFO/Ultimate VPN:(364): Starting Connect.....
04-15 17:09:07.501: INFO/Ultimate VPN:(364): Into onPostExecute()
04-15 17:09:07.501: INFO/Ultimate VPN:(364): FINISHED onPostExecute()
04-15 17:09:07.501: INFO/Ultimate VPN:(364): Into Start Timer
04-15 17:09:07.542: INFO/MON CONN(364): Into StartMonitor
04-15 17:09:07.542: INFO/MON CONN(364): Into checkConnection
04-15 17:09:07.671: WARN/InputManagerService(69): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4073b170
04-15 17:09:08.252: INFO/System.out(364): MonitorConnection : Notifing the GUI
04-15 17:09:08.262: INFO/Ultimate VPN:(364): GOT MESSAGE FROM MonitorConnection
04-15 17:09:08.262: INFO/Ultimate VPN:(364): Preparing to Start
04-15 17:09:18.322: INFO/Ultimate VPN:(364): ABOUT TO RE-CONNECT
04-15 17:09:18.322: INFO/Ultimate VPN:(364): Inside Connectclicked
04-15 17:09:18.322: INFO/Ultimate VPN:(364): isConnectionEligible = true
04-15 17:09:18.331: WARN/dalvikvm(364): threadid=11: thread exiting with uncaught exception (group=0x40015560)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): FATAL EXCEPTION: Timer-0
04-15 17:09:18.351: ERROR/AndroidRuntime(364): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at android.os.Handler.<init>(Handler.java:121)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at android.app.Dialog.<init>(Dialog.java:101)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at android.app.AlertDialog.<init>(AlertDialog.java:63)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at android.app.ProgressDialog.<init>(ProgressDialog.java:80)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at android.app.ProgressDialog.<init>(ProgressDialog.java:76)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.StartUltimate$ConnectTask.<init>(StartUltimate.java:386)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.StartUltimate.ConnectClicked(StartUltimate.java:324)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.StartUltimate.ReConnect(StartUltimate.java:340)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.StartUltimate.notifyUI(StartUltimate.java:316)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.MonitorConnection.setConnected(MonitorConnection.java:43)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.MonitorConnection.checkConnection(MonitorConnection.java:69)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.MonitorConnection.StartMonitor(MonitorConnection.java:61)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.MonitorConnection.run(MonitorConnection.java:52)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at java.util.Timer$TimerImpl.run(Timer.java:284)
04-15 17:09:18.382: WARN/ActivityManager(69): Force finishing activity orange.android.vpn/.StartUltimate
知道我为什么会收到这个错误吗?我该如何解决?我尝试在ConnectTask类而不是Activity类中处理UINotifier。但是在StartConnect()之后立即调用onPost,尽管已经添加了StartTimer并且该线程已经在运行。然后doInBackground也停止了,所以我用这种方式管理它。但是我得到错误的方式。在其他情况下,我得到...... Looper ....例外。
非常感谢任何帮助。
由于
答案 0 :(得分:1)
我会摆脱UINotifier(自定义实现),并使用Android中现成的Handler机制来通知用户界面。
有关类似的实施,请参阅How to get XML using AsyncTask and Timer?。
另外,请参阅此处的无痛线程文章:http://developer.android.com/resources/articles/painless-threading.html,了解有关线程(和处理程序)的更多信息