D
D
Dutymy2021-08-13 01:18:02
Android
Dutymy, 2021-08-13 01:18:02

How to leave the service after closing the application?

Hello, I'm making my first screen marker
and I need the overlay to remain even after closing the application, but so that I can enable / disable the overlay from a button in the application (if the application is open)
How can I implement this
I use

Intent overlayServiceIntent = new Intent(this, OverlayService.class);
bindService(overlayServiceIntent, overlayConnection, Context.BIND_AUTO_CREATE);

to start the service
and
Intent overlayServiceIntent = new Intent(this, OverlayService.class);
stopService(overlayServiceIntent);

to stop
(onDestroy is called, but the button does not disappear) the button also disappears when the application is closed (and I want it to remain even when the application is closed - how to implement this) (as far as I understand, using startService, I cannot close the service from the application ... or I wrong...)
the overlay class itself
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class OverlayService extends Service {
    Button overlayedButton;

    WindowManager wm;


    @Override
    public IBinder onBind(Intent intent) {

        Toast.makeText(this, "Hi", Toast.LENGTH_LONG).show();


        wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        overlayedButton = new Button(this);
        overlayedButton.setText("Overlay button");
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
        wm.addView(overlayedButton, params);

        return null;
    }

    public void onDestroy() {
        super.onDestroy();

        Toast.makeText(this, "Bye", Toast.LENGTH_LONG).show();

        wm.removeView(overlayedButton);
        overlayedButton = null;

    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question