Home > database >  Call intent of another application without blocking the UI
Call intent of another application without blocking the UI

Time:11-13

Our Android application needs to communicate with another application. The recommended way to talk to the other application is using an intent. Our MainActivity looks like the following:

@Override
protected void onCreate(Bundle savedInstanceState) {
...
new Thread(new Runnable() {
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {

            callIntent();
            try {
                Thread.sleep(20 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}).start();
... 
}

ActivityResultLauncher<Intent> myActivityResultLauncher;

void callIntent() {
    Intent intent = new Intent("application-code"); 
    intent.setType("text/plain");
    intent.putExtra(android.content.Intent.EXTRA_TEXT,"config and metadata");
    myActivityResultLauncher.launch(intent);
}

and registerForActivityResult is called as well to receive the data from this intent. As you can see we cal the Intent each 20 seconds. The intent, we are calling normally shows a UI pop up, but we pass parameters to it to disable that pop up.

Launching the intent from our application blocks the UI Thread and our App is hanging until the intent finishes its job. How can we make this intent run in the background threads and not block the UI?

The UI of our Application is running a simple WebView and it losses focus completely, while the intent is executing. Executing the intent takes a few seconds to complete. After the few seconds, the website in the WebView is responsive again. This is why we think it is blocking the UI Thread. (The intent itself is using a bluetooth printer to print some information, if this is helpful)

Further research, led us to believe that the "invisible" Intent could be getting the focus (not the Webview) and hence it seems that our UI is blocked, but its just in the background, but we could not prove or disprove this.

CodePudding user response:

The UI of our Application is running a simple WebView and it losses focus completely, while the intent is executing

You are starting an activity. Your current activity will lose focus as a result. This is perfectly normal.

The intent, we are calling normally shows a UI pop up, but we pass parameters to it to disable that pop up.

The "pop up" still happens. It may not render anything on the screen, but Android still allocates a window for the activity that you are starting, and that window will take over foreground input and focus.

Further research, led us to believe that the "invisible" Intent could be getting the focus (not the Webview) and hence it seems that our UI is blocked, but its just in the background, but we could not prove or disprove this.

Your research is correct. You can determine this by logging the result of relevant lifecycle methods. For example, you could log when onPause() and onResume() get called — you will see that onPause() is called when you start the other activity and that onResume() is called when input focus returns to your current activity.

  • Related