Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PopUp dialog Android from background thread

I need a popup dialog to be shown when i get a message from different thread but the dialog should be not dependent on Activity i.e, it should display the dialog wherever the screen focus is.

Can it be done? Because the dialog is handled per Activity, I thought of using a service but again it would be one more thread added and I want to avoid that.

Any other options available?

like image 427
Sam97305421562 Avatar asked Jun 22 '09 13:06

Sam97305421562


3 Answers

If you're trying to ask how to show a dialog when your activity is not the focused activity on the user's phone then try using Notifications instead. Popping up a dialog over a different application interrupts the user when they may be doing something else. From the Android UI guidelines:

Use the notification system — don't use dialog boxes in place of notifications

If your background service needs to notify a user, use the standard notification system — don't use a dialog or toast to notify them. A dialog or toast would immediately take focus and interrupt the user, taking focus away from what they were doing: the user could be in the middle of typing text the moment the dialog appears and could accidentally act on the dialog. Users are used to dealing with notifications and can pull down the notification shade at their convenience to respond to your message.

A guide to create notifications is here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

like image 159
Intrications Avatar answered Nov 05 '22 10:11

Intrications


Alternative solution :

AlertDialog dialog;
//add this to your code
    dialog = builder.create();
    Window window = dialog.getWindow(); 
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.token = mInputView.getWindowToken();
    lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
    window.setAttributes(lp);
    window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//end addons
alert.show();
like image 1
Maher Abuthraa Avatar answered Nov 05 '22 10:11

Maher Abuthraa


if I understand you correctly you could use a base class for all of your activities

public abstract class BaseActivity extends Activity{
    protected static BaseActivity current_context = null;

    @override
    protected void onPause(){
        current_context = null;
        super.onPause();
    }

    @override
    protected void onResume(){
        current_context = this;
        super.onResume();
    }

    public static void showDialog(/*your parameters*/){
        //show nothing, if no activity has focus
        if(current_context == null)return;
        current_context.runOnUiThread(new Runnable() {
            @override
            public void run(){
                AlertDialog.Builder builder = 
                    new AlertDialog.Builder(current_context);
                //your dialog initialization
                builder.show();
            }
        });
    }
}

in your thread show your dialog with BaseActivity.showDialog(..) But this approach doesn't work if you want to show your dialog on top of any activity of the target device.

like image 1
mini Avatar answered Nov 05 '22 12:11

mini