java关闭应用程序后如何离开服务?
我正在做我的第一个屏幕标记
即使在应用程序关闭后,我也需要覆盖保持不变,但这样我可以通过应用程序中的按钮启用/禁用覆盖(如果应用程序打开)
我如何实现这一点
我用
Intent overlayServiceIntent = new Intent(this, OverlayService.class);
bindService(overlayServiceIntent, overlayConnection, Context.BIND_AUTO_CREATE);
启动服务 及
Intent overlayServiceIntent = new Intent(this, OverlayService.class);
stopService(overlayServiceIntent);
制止
(onDestroy被调用,但按钮不会消失)当应用程序关闭时,按钮也会消失(我希望即使在应用程序关闭时按钮也会保持不变)(据我所知,使用startService,我无法从应用程序关闭服务…或者我错了…)
覆盖类本身
import 安卓.app.Service;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.graphics.PixelFormat;
import 安卓.os.IBinder;
import 安卓.view.WindowManager;
import 安卓.widget.Button;
import 安卓.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;
}
}
共 (0) 个答案