2012-07-01 65 views
0

我目前使用扩展AsyncTask的类将一些数据发布到php webservice。单独线程上的同步HttpPost

现在我需要进行同步调用,因为我想显示数据发送后我的应用程序崩溃的默认异常弹出窗口(previousHandler.uncaughtException(t, e))。

我可以只对AsyncTask禁用异步功能,或者你有任何其他建议

这里是我当前的代码,在此先感谢:

public void uncaughtException(Thread t, Throwable e) { 

    final Writer result = new StringWriter(); 
    final PrintWriter printWriter = new PrintWriter(result); 
    e.printStackTrace(printWriter); 
    String stacktrace = result.toString(); 
    printWriter.close(); 

    String deviceUuid = Utilities.DeviceUuid(ctx, cr); 
    String bluetoothName = Utilities.LocalBluetoothName(); 

    AsyncTasks.ErrorLogTask logTask = new AsyncTasks.ErrorLogTask(e, bluetoothName, deviceUuid, stacktrace); 
    logTask.execute(); 

    //task wont be complete before this code below runs since its async. 

    previousHandler.uncaughtException(t, e); 


} 

回答

0

我觉得你还是需要捕捉例外。我会保持你的AsyncTask的调用,但赶上例外。使用标志作为doInBackground的返回值,或者创建AsyncTask的本地变量作为标志,并在onPostExecute中检查标志并在必要时显示对话框。

一般而言,您不希望在主UI线程上进行服务器调用,因为存在ANR风险。看到这篇文章: http://developer.android.com/guide/practices/responsiveness.html

0

把你的默认异常弹出方法ErrorLogTask。当您的任务完成时,该块将在主线程中执行。