0

在进行一些初始化工作时,我需要发送一些警报短信。因此,我在 android 应用程序的主要活动中使用了以下代码。

AlertDialog alertDialog;
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i("main","=================init===============");
    alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Title Info");
    alertDialog.setMessage("Initilizing so please wait....");
    alertDialog.show();
    // calling some initilizing function from jni which takes some time
    alertDialog.hide(); 
   }

当我运行应用程序时,我在 logcat 中获得了该日志,但该警报对话框没有显示..我不明白为什么这不起作用?

4

3 回答 3

5

这是因为当您向 .show() 询问警报对话框时,它告诉框架在系统下次获得控制权时显示对话框(即在您的方法将控制权返回给应用程序之后),因此对话框显示不是立即完成。

因为您已经调用了 show 并在此方法中立即进行了 hide,所以当应用程序尝试绘制对话框时,对话框状态将处于“隐藏”状态。

如果要显示并稍后隐藏对话框,则需要稍后调用 hide,例如在计时器上或由于 alertDialog 的某些回调。(还要确保您在主线程中调用显示和隐藏)

于 2013-01-21T13:52:14.983 回答
2
dialogbox = new Dialog(alertdialog.this);
                dialogbox.setContentView(R.layout.exit_dialog_box);
                dialogbox.setTitle("");
                dialogbox.setCancelable(true);
                Button button = (Button) dialogbox.findViewById(R.id.Button);

                dialogbox.show();
于 2013-01-21T13:54:51.733 回答
1

您可以使用 android 中的 AsyncTask 方法执行此操作。使用异步创建一个类

私有类 ShowDialogAsyncTask 扩展 AsyncTask {

    @Override
    protected void onPreExecute() {
        // update the UI immediately after the task is executed
        super.onPreExecute();
        alertDialog = new AlertDialog.Builder(MainClass.this).create();
        alertDialog.setTitle("Title");
        alertDialog.setMessage("msg....");
        alertDialog.show();

    }

    @Override
    protected Void doInBackground(Void... params) {
        //Perform your JNI operation
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        alertDialog.hide();

    }
}

在你的课上

警报对话框警报对话框;公共无效 onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//添加以下代码

新的 ShowDialogAsyncTask().execute();

}
于 2013-01-22T12:44:18.730 回答