gpt4 book ai didi

android - Stopself 不停止服务 Android

转载 作者:行者123 更新时间:2023-11-29 16:56:33 26 4
gpt4 key购买 nike

我正在尝试下面的代码

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();
}


}

我试过stopself();,this.stopself(),stopForeground(true);,没有 startForeground,但是服务还是没有停止,如何停止这个服务

按钮被点击,它显示 toast ,但服务仍然没有关闭

它有一个窗口管理器,用于在屏幕上创建一个窗口

最佳答案

删除屏幕上的窗口管理器

喜欢

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

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


break;
}

关于android - Stopself 不停止服务 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44858430/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com