Android服务不会停止自我

时间:2015-02-24 20:09:07

标签: android service android-windowmanager

这是我的代码:

               @Override public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                // Get current time in nano seconds.
                long pressTime = System.currentTimeMillis();


                // If double click...
                if (pressTime - lastPressTime <= 300) {
                    createNotification();
                    ServiceFloating.this.stopSelf();
                    mHasDoubleClicked = true;
                }
                else {     // If not double click....
                    mHasDoubleClicked = false;
                }
                lastPressTime = pressTime; 
                initialX = paramsF.x;
                initialY = paramsF.y;
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                    if(event.getRawX() - initialTouchX >40)
                {
                    paramsF.x = (int)dispwidth;
                    paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
                    ObjectAnimator animY = ObjectAnimator.ofFloat(layout, "translationX", 100f, 0f);
                    animY.setDuration(1000);//1sec
                    animY.setInterpolator(new BounceInterpolator());
                    animY.setRepeatCount(0);
                    animY.start();
                }
                else if(initialTouchX-event.getRawX()>40){
                    paramsF.x = 0;
                    paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
                    ObjectAnimator animY = ObjectAnimator.ofFloat(layout, "translationX", -100f, 0f);
                    animY.setDuration(1000);//1sec
                    animY.setInterpolator(new BounceInterpolator());
                    animY.setRepeatCount(0);
                    animY.start();
                }
                windowManager.updateViewLayout(layout, paramsF);
                break;
            case MotionEvent.ACTION_MOVE:
                paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
                paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
                windowManager.updateViewLayout(layout, paramsF);
                break;
            }
            return false;
        }
    });
} catch (Exception e) {
    // TODO: handle exception
}

这是我的onDestroy()

    @Override
public void onDestroy() {
    super.onDestroy();
    if (safe != null) windowManager.removeView(safe);
}

safeLinearLayout,我在oncreate中附加了windowmanager对象 该服务启动并运行正常。当我从主要活动点击按钮时调用stopService(new Intent(MainActivity.this, ServiceFloating.class));我可以停止服务但是当我尝试从它自己的视图停止服务时,应用程序崩溃并重新启动Fatal signal 11 error

如何从自己的视图中停止服务?

1 个答案:

答案 0 :(得分:0)

尝试从服务内部停止服务,例如在按下按钮时从活动向服务发送消息。您需要将活动绑定到服务才能发送和接收消息。

当服务从活动收到停止消息时,我使用此方法:

stopForeground(true);
stopSelf();