不要停止服务Android

时间:2017-07-01 07:23:25

标签: android service

我正在尝试关注代码

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,但服务仍未停止,我该如何停止此服务

单击该按钮并显示吐司,但仍未关闭服务

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

1 个答案:

答案 0 :(得分:3)

删除屏幕上的窗口管理器

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

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


                    break;
     }
相关问题