如何以最终用户友好的方式处理异常

时间:2012-09-07 12:35:31

标签: android

我正在开发一个Android应用程序,我正在处理向我们的服务器发送信息和从我们的服务器接收信息。请在发生任何类型的异常时让我知道我正在尝试显示警告框,但是问题是警告框不会始终显示。请帮助我解决方案,以便在发生异常时显示某种友好的UI。 我正在使用的AlertBox

public  void Alertbox(String title, String mymessage,Context context ) {
    new AlertDialog.Builder(context)
    .setMessage(mymessage)

    .setCancelable(true)
    .setNeutralButton(android.R.string.ok,new DialogInterface.OnClickListener()

    {
        public void onClick(DialogInterface dialog,int whichButton) {



        }
    }).show();
}

public static void ProcessDialogue(String title,Context context)
{


}

1 个答案:

答案 0 :(得分:1)

使用Extras字符串创建ErrorBroadcastReceiver和错误操作。

public class ErrorReceiver extends BroadcastReceiver {
    public static final String ERROR_ACTION = "com.yourpackage.ERROR_ACTION";
    public static final String ERROR_MESSAGE = "ERROR_MESSAGE"; 

    @Override
    public void onReceive(Context context, Intent intent) {
        String errorMessage = intent.getStringExtra(ERROR_MESSAGE);     
        Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show();
    }

}

并向此接收器发送广播意图

 Intent errorIntent = new Intent(ERROR_ACTION);
        errorIntent.putExtra(ERROR_MESSAGE , e.getMessage());// exception from try/catch block or your custom error type
        sendBroadcast(errorIntent)