0

I created an overlay view from this tutorial : http://www.piwai.info/chatheads-basics/

This is my Service Class:

public class CustomService extends Service{

    private WindowManager mWindowsManger;
    private ImageView mFloatingImage;
    private WindowManager.LayoutParams mParams;



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

    @Override
    public void onCreate() {
        super.onCreate();
        mWindowsManger = (WindowManager) getSystemService(WINDOW_SERVICE);

        mFloatingImage= new ImageView(this);
        mFloatingImage.setImageResource(R.mipmap.ic_launcher);
        mFloatingImage.setAlpha(0.5f);
        mParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        mParams.gravity = Gravity.TOP | Gravity.LEFT;
        mParams.x = 0;
        mParams.y = 100;
        mWindowsManger.addView(mFloatingImage, mParams);

        mFloatingImage.setOnTouchListener(new CustomTouchListener(this));

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mFloatingImage!= null) mWindowsManger.removeView(mFloatingImage);
    }
}

And this is my activity where CustomService starts

public class MainActivity extends ActionBarActivity {


    @Override
    protected void onStart() {
        super.onStart();
        startService(new Intent(this, CustomService.class););
    }
...
}

Now my problem is whenever i close activity ,my service stopped and disappeared or onCreate method of CustomService called and it's going to recreating it.

Any ideas?

Saeed Masoumi
  • 8,746
  • 7
  • 57
  • 76

1 Answers1

2

you have to override onStartCommand and return START_STICKY in of your service.

something like:

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

     return START_STICKY;
}

Also make sure to declare your service in the manifest:

<service android:name = ".YourServiceClass"></service> 

Also move your work code from onCreate to onStartCommand and see if it makes any changes.

Also make sure you are not doing very heavy work in the service as the OS will destroy services which are using too much resources if the phone/emulator is low on mem/cpu

Another thing to do is start the service in the Application level, that is extending the Application class and put your global things in there

Dumbo
  • 13,555
  • 54
  • 184
  • 288
  • Same problem appears. My `Service` stopped – Saeed Masoumi Mar 27 '15 at 15:15
  • @Saeed I'v added some more tips maybe they help – Dumbo Mar 27 '15 at 15:47
  • Thanks but problem appears again – Saeed Masoumi Mar 27 '15 at 16:01
  • @Saeed tell me how you notice the service stops? Put some logging in onStop of your activity and onDestroy of your service to know when exactly service stops. Try with different device/emulator and see if its the same – Dumbo Mar 27 '15 at 17:03
  • It didn't come to `onStop` or `onDestroy`. As you see from my code `mFloatingImage` should be displayed on `Windows` so sometimes it recreates service (going to `onStartCommand` and `onCreate`) and sometimes there is no `ImageView` on windows (i do some logging but in this case there is nothing in logcat) and i think my service stopped ( i tested in different devices and same problem happen).Finally i find a better way : http://pingpongboss.github.io/StandOut/ but i don't know why this problem happens – Saeed Masoumi Mar 27 '15 at 19:51
  • So then I guess its not the service which stops...there was something wrong about the logic behind showing up the overlay – Dumbo Mar 29 '15 at 14:28