Java 从后台线程弹出对话框Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1027149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PopUp dialog Android from background thread
提问by Sam97305421562
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.
当我从不同线程收到消息时,我需要显示一个弹出对话框,但该对话框不应依赖于 Activity,即,它应该在屏幕焦点所在的任何位置显示对话框。
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?
还有其他选择吗?
回答by Intrications
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:
如果您想询问当您的活动不是用户手机上的重点活动时如何显示对话框,请尝试使用通知。当用户可能正在做其他事情时,在不同的应用程序上弹出一个对话框会打断用户。来自Android UI 指南:
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.
使用通知系统——不要使用对话框代替通知
如果您的后台服务需要通知用户,请使用标准通知系统——不要使用对话框或 Toast 来通知他们。对话或吐司会立即吸引注意力并打断用户,将注意力从他们正在做的事情上移开:用户可能在对话框出现的那一刻正在输入文本,并且可能会意外地对对话框进行操作。用户习惯于处理通知,可以在方便时拉下通知栏以回复您的消息。
A guide to create notifications is here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
创建通知的指南在这里:http: //developer.android.com/guide/topics/ui/notifiers/notifications.html
回答by Maher Abuthraa
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();
回答by mini
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 anyactivity of the target device.
在你的线程中显示你的对话框BaseActivity.showDialog(..)
但是如果你想在目标设备的任何活动之上显示你的对话框,这种方法不起作用。