You have to keep some things in your mind when creating your gui:
All time-consuming work without gui should be done in the background (using asyncTask, services, threads, etc.).
GUIs (such as views and other graphical objects) should always be created and updated from UIthreads.
Thus, the create and display dialog is the work of the user interface, so it should be in the user interface thread. You can use various methods to run any code in the user interface thread, for example:
- You can use runOnUiThread to run a piece of code in a user interface thread.
- You can use messageHandler.
- You can use the broadcast sender and receiver.
I think runOnUiThread is best used for your case
runOnUiThread (new Runnable() { public void run () {
To understand the concept, this can help you:
http://developer.android.com/guide/components/processes-and-threads.html
To create a broadcast, you can use:
Step 1. Create a broadcast receiver in your activity from the place where you start your service.
BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("create_dialog")) {
Step 2. Register your receiver after creating your broadcast receiver.
IntentFilter filter = new IntentFilter("create_dialog"); registerReceiver(receiver, filter);
Step 3. Transfer the broadcast from the service to display a dialog box.
Intent intent = new Intent("create_dialog"); intent.putExtra("dialog_data", data_object to display in dialog); SendBroadcast(intent);
Hope this helps you.
source share