1

在使用 stopSelf(); 激活侦听器后,我需要停止此服务;但是,即使没有 Drag 事件,服务也会结束...如何在调用 StopService 之前检测是否发生了 Drag 事件?

           mainButton.setOnTouchListener(new MyTouchListener());
           vTopLeft.setOnDragListener(new MyDragListener());
           vTopRight.setOnDragListener(new MyDragListener());
           vBottomLeft.setOnDragListener(new MyDragListener());
           vBottomRight.setOnDragListener(new MyDragListener());
           Log.d("tok", "add mview");
           stopSelf(); ---> this should wait after draglistener is finish.

MyDragListener.java

Public class MyDragListener implements View.OnDragListener  {


@Override
public boolean onDrag(View v, DragEvent event) {
    String containerName = null;
    int ID = 0;
    int Dragaction = event.getAction();
    int gravity2 = 1234567879;

    switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:

            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            break;
        case DragEvent.ACTION_DROP:

            View view = (View) event.getLocalState();

            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            owner.setVisibility(View.GONE);
            FrameLayout container = (FrameLayout) v;
            container.addView(view);

            LocationSerializable ls = new LocationSerializable();
            switch (v.getId()) {
                case R.id.topLeft:


                    gravity2 = Gravity.LEFT | Gravity.TOP;

                    ls.setGravity(gravity2);
                    break;

                case R.id.topRight:

                    gravity2 = Gravity.RIGHT | Gravity.TOP;
                    ls.setGravity(gravity2);
                    break;

                case R.id.bottomLeft:
                    gravity2= Gravity.BOTTOM | Gravity.LEFT;
                    ls.setGravity(gravity2);

                    break;
                case R.id.bottomRight:
                    gravity2 = Gravity.RIGHT | Gravity.BOTTOM;
                    ls.setGravity(gravity2);
                    break;

                default:
                    throw new RuntimeException("Unknown ID");
            }

            LinearLayout containerParent = (LinearLayout) v.getParent();

            LinearLayout conatainerGrandMother = (LinearLayout) containerParent.getParent();

            conatainerGrandMother.setVisibility(View.GONE);




        case DragEvent.ACTION_DRAG_ENDED:

        default:


            break;
    }
    return false;
    }
}
4

1 回答 1

1

Stop your service in case DragEvent.ACTION_DRAG_ENDED:

below is a sample template.

 @Override
      public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
        // do nothing
          break;
        case DragEvent.ACTION_DRAG_ENTERED:
            break;
        case DragEvent.ACTION_DRAG_EXITED:        

          break;
        case DragEvent.ACTION_DROP:
          // Dropped, reassign View to ViewGroup

          break;
        case DragEvent.ACTION_DRAG_ENDED:
         // option 1
          Context.stopService();
           // option 2
            // stopService(new Intent(Activity.this,MailService.class));
           //stopSelf();
            //  other alternative is
             //  v.getContext().stopService(new Intent(v.getContext(),DragDropButtonMainService.class));
          break;
        }
        return true;
      }

According to android docs

After the user releases the drag shadow, and after the system sends out (if necessary) a drag event with action type ACTION_DROP, the system sends out a drag event with action type ACTION_DRAG_ENDED to indicate that the drag operation is over.

于 2013-10-24T05:09:24.950 回答