Java – whether UI threads can be blocked when the alert dialog box is displayed

I have this method. Basically, I wait for the items in the singleton queue to become empty. A background service is running. Once it deletes all items in the queue and processes them one by one, the service will stop This code runs in the main thread. What happens when I call wait here? Does the alert dialog box still display and prevent the user from performing any other actions?

void waitForService() {
    openConnectionToUploadQueue();
    if(answersQueue.getCount(objInterviewQuestion.getQid()) <= 0){
        answersQueue.close();
        return;
    }
    if(!answersQueue.isInterviewUploadServiceRunning()) {
        answersQueue.startInterviewUploadService();
    }
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(getString(R.string.auto_submit_alert_title));
    builder.setCancelable(false);
    builder.setMessage(R.string.uploading_pending_answers);
    AlertDialog waitForServiceDialog = builder.create();
    waitForServiceDialog.show();
    while (answersQueue.getCount(objInterviewQuestion.getQid()) > 0) {
        // do nothing and keep loop running till answersQueue is empty
    }
    waitForServiceDialog.dismiss();
}

Solution

You should never block UI threads When you hold the UI thread for too long, the system will display a dialog box indicating that XXX is not responding and asking the user to terminate your application

Instead, you should use the callback style call, and when the service starts and you receive a method call from the callback, you will close the dialog box

Edit:

As mentioned above, you need to implement broadcastreceiver

This is a demonstration project of my other things. You can use it as an example of how to create and use broadcastreceiver

https://github.com/cyfung/ActivityRecognitionSample

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>