0
public class MyCustomService extends Service {
public static final String INPUT_TEXT="INPUT_TEXT";
public static final String OUTPUT_TEXT="OUTPUT_TEXT";
private volatile HandlerThread mHandlerThread;
private ServiceHandler mServiceHandler;
private WindowManager windowManager;
private ImageView chatHead;
private static Context mContext;
//public Socket client;


IBinder mBinder = new LocalBinder();

public static Socket client;


@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

public class LocalBinder extends Binder {
    public MyCustomService getServerInstance() {
        return MyCustomService.this;
    }
}


private final class ServiceHandler extends Handler {
    public ServiceHandler(Looper looper) {
        super(looper);
    }


    @Override
    public void handleMessage(Message message) {

    }
}


// Fires when a service is first initialized
public void onCreate() {
    super.onCreate();
    this.mContext = this;


    mHandlerThread = new HandlerThread("MyCustomService.HandlerThread");
    mHandlerThread.start();
    // An Android service handler is a handler running on a specific background thread.
    mServiceHandler = new ServiceHandler(mHandlerThread.getLooper());



}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    mServiceHandler.sendEmptyMessageDelayed(0, 500);

    mServiceHandler.post(new Runnable() {
        @Override
        public void run() {
            // Do something here in background!

            IO.Options opts = new IO.Options();
            opts.query = "auth_token=51";
            try {
                client = IO.socket("http://192.168.0.106:3000/",opts);
                client.on("message", onMessage);

                client.connect();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }


            // If desired, stop the service
            //stopSelf();
        }
    });

    return START_STICKY;
}


@Override
public void onDestroy() {
    // Cleanup service before destruction
    client.disconnect();
    client.close();
    mHandlerThread.quit();
    if (chatHead != null) windowManager.removeView(chatHead);
}


private Emitter.Listener onMessage = new Emitter.Listener() {
    @Override
    public void call(Object... args) {

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        chatHead = new ImageView(mContext);
        chatHead.setImageResource(R.drawable.pro2);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;

        windowManager.addView(chatHead, params);


    }
};

}

i just use socket inside service and add listener when message arrived i call function

onMessage

but its give me an error and its

Can't create handler inside thread that has not called Looper.prepare()

so when message arrived its show this error

i think the problem with the thread any idea ?

medo
  • 479
  • 3
  • 9
  • 24

1 Answers1

0

This is a common problem, probably it's caused because you're trying to modify UI from a wrong thread. The UI has an exclusive thread and you'll get these kind of errors if you don't follow this rule.

There are several ways of running code in the UI thread, you can find more detail here: Android basics: running code in the UI thread

Hope this helps!

Community
  • 1
  • 1
Pato94
  • 784
  • 5
  • 6
  • same problem its happen only when i use `windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);` `windowManager.addView(chatHead, params);` – medo Jun 28 '16 at 20:16