问题描述
我具有背景Service,其与任何应用程序(或活动)没有绑定.它正在监控传入的消息,以及接收消息,我必须显示一个对话框弹出窗口.我尝试定义一个用于显示警报对话框的方法,但它会使"Can't create handler inside thread that has not called Looper.prepare()"表示异常 关于如何解决这个问题,我真的没有任何想法.我在这里搜索了许多帖子,但没有得到如何解决这个问题.帮助高度赞赏.谢谢.
推荐答案
单独地创建一个处理程序.
private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { //Call you method in This one... displayDialog(); or whatever method name is } };
现在在哪里调用displaydialog()方法或命名为什么.替换以下代码.
handler.sendEmptyMessage(0);
2 ND POSIBLE解决方案是:
将您的方法调用以下内容:
runOnUiThread(new Runnable(){ public void run(){ //Call the method here } });
希望这有助于
谢谢 沙赫
其他推荐答案
可能是重复的问题
如果要从后台服务打开一个对话框,那么您不能这样做,但有一种方法可以从服务中打开您的活动,以获取更多信息和代码
按照以下链接
问题描述
I am having a background Service which is not binded to any of the application(or Activity). It's monitoring incoming messages, and on receiving message, I have to show one dialog popup. I have tried defining one method which is for showing alert dialog, but it gives exception as "Can't create handler inside thread that has not called Looper.prepare()" I really don't have any idea regarding how to solve this problem. I have searched many post like this over here but not getting how to resolve this one. Help highly appreciated. Thanks.
推荐答案
Create a Handler seperately like this.
private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { //Call you method in This one... displayDialog(); or whatever method name is } };
Now where you are calling the displayDialog() method or what ever named dialog. replace the following code.
handler.sendEmptyMessage(0);
2nd Posible Solution is :
put your method calling code inside the following:
runOnUiThread(new Runnable(){ public void run(){ //Call the method here } });
Hope this helps
Thanks sHaH
其他推荐答案
Might be repeated question
If you want to open a dialog from background service then you can not do but there is a way you can open your activity as dialog from service for more info and code
follow below link