In my Activity class implements UINotifier that is used to notify the Activity class on call of a TimerTask object. In my doInBackground(), I call my function located in ASyncTask class (connectTask) only. onPostExecute is executed and in last, I call a method "ReConnect()" that is located in my Activity class. The code and its flow is :
// 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 is called, and MonitorThread notifies Activity class and that calls ReConnect which inturn calls ConnectClicked. In ConnectClicked, connectTask = new ConnectTask(); is where I get this exception and the log :
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.St开发者_运维知识库artUltimate.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
Any idea why I get this error ? How do I solve it ? I tried handling the UINotifier in ConnectTask class instead of Activity class. But the onPost is called immediately after StartConnect() , despite StartTimer was added to it and that thread was already running. Then also the doInBackground stops, so I managed it this way. But both the ways I get error. In other case I get ... Looper.... exception.
Any help is highly appreciated.
Thanks
I would get rid of the UINotifier (custom implementation) and look at using the out-of-the box Handler mechanism in Android to notify the UI.
See How to get XML using AsyncTask and Timer? for a similar implementation.
Also, see the Painless threading article here : http://developer.android.com/resources/articles/painless-threading.html for more information about threading (and Handlers)
精彩评论