1

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.

Balaji Khadake
  • 3,449
  • 4
  • 24
  • 38

2 Answers2

2

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

Shah
  • 4,990
  • 10
  • 48
  • 70
  • Still i m getting exception as "Can't create handler inside thread that has not called Looper.prepare()". I am not getting concept of Looper, can you help me? – Balaji Khadake Aug 05 '11 at 12:44
  • on my side runOnUIThread() works perfectly fine... you should try that – Shah Aug 09 '11 at 03:08
0

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

android:chat app popup view

Community
  • 1
  • 1
Dharmendra
  • 33,296
  • 22
  • 86
  • 129