1234567
1234567

Reputation: 2485

Stopself not stopping service Android

I am trying following code

TxtService extends Service   implements View.OnClickListener{
    private RelativeLayout floatingControls;
    private View controls;
    private ImageButton  CloseMainButton;


    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        floatingControls = (RelativeLayout) li.inflate(R.layout.paintimgtxtservice, null);
        controls = floatingControls.findViewById(R.id.controls);
        CloseMainButton = (ImageButton) controls.findViewById(R.id.CloseMainButton);
        CloseMainButton.setOnClickListener(this);
        final 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.START;
        windowManager.addView(floatingControls, params);
        return START_NOT_STICKY;
    }

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

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.CloseMainButton:
                stopForeground(true);
                this.stopSelf();
                Toast.makeText(PaintImgTxtService.this, "stop", Toast.LENGTH_SHORT).show();
                break;
 }
 }

    @Override
    public void onDestroy()
    {


        super.onDestroy();
    }


}

I have tried stopself();, this.stopself(), stopForeground(true);, there is no startForeground, but still the service doesnot stop, how can I stop this service

The button is clicked and it shows the toast but still service is not closed

It has a window manager for creating a window over the screen

Upvotes: 1

Views: 1038

Answers (1)

Vishal Bhut
Vishal Bhut

Reputation: 122

remove window manager on screen

Like

 public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.CloseMainButton:

                   if (floatingControls!= null)
                   windowManager.removeView(floatingControls);
                    this.stopSelf();


                    break;
     }

Upvotes: 4

Related Questions