2014-01-20 1 views
0

주요 활동에는 블루투스 서비스와 메시지를 교환하는 처리기가 있습니다. 이것은 잘 작동합니다. 이제 두 번째 활동 (SurfaceViewAnimation)을 시작하겠습니다. 나는 다음과 같이한다 :두 번째 활동의 속성을 주 활동에서 어떻게 변경합니까?

startActivity (새로운 인 텐트 (this, SurfaceViewAnimation.class));

하지만 블루투스에 대한 명령을 받으면 주 활동에서 SufaceViewAnimation 클래스의 일부 속성을 변경하고 싶습니다.

어떻게하면됩니까?

SufaceViewAnimation 클래스의 코드는 다음과 같습니다

class BouncingBallView extends SurfaceView implements SurfaceHolder.Callback { 

    private BouncingBallAnimationThread bbThread = null; 

    private int xPosition = getWidth()/2; 
    private int yPosition = getHeight()/2; 
    private int xDirection = 20; 
    private int yDirection = 40; 

    private static int radius = 20; 
    private static int ballColor = Color.RED; 

    public BouncingBallView(Context ctx, AttributeSet attrs, int defStyle) { 
      super(ctx, attrs, defStyle); 
      getHolder().addCallback(this); 
    } 

    public void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 
      Paint paint = new Paint(); 
      paint.setColor(Color.BLACK); 
      canvas.drawRect(0,0,getWidth(),getHeight(), paint); 
      paint.setColor(ballColor); 
      canvas.drawCircle(xPosition, yPosition, radius, paint); 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
      if (bbThread!=null) return; 
      bbThread = new BouncingBallAnimationThread(getHolder()); 
      bbThread.start(); 
    } 

    public void surfaceChanged(SurfaceHolder holder,int format, int width, int height) { } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
      bbThread.stop = true; 
    } 

    private class BouncingBallAnimationThread extends Thread { 

      public boolean stop = false; 
      private SurfaceHolder surfaceHolder; 

      public BouncingBallAnimationThread(SurfaceHolder surfaceHolder) { 
        this.surfaceHolder = surfaceHolder; 
      } 

      public void run() { 
        while (!stop) { 
          xPosition += xDirection; 
          yPosition += yDirection; 
          if (xPosition<0) { 
            xDirection = -xDirection; 
            xPosition = radius; } 
          if (xPosition>getWidth()-radius) { 
            xDirection = -xDirection; 
            xPosition = getWidth()-radius; } 
          if (yPosition<0) { 
            yDirection = -yDirection; 
            yPosition = radius; } 
          if (yPosition>getHeight()-radius) { 
            yDirection = -yDirection; 
            yPosition = getHeight()-radius-1; } 

          Canvas c = null; 
          try { 
            c = surfaceHolder.lockCanvas(null); 
            synchronized (surfaceHolder) { 
              onDraw(c); 
            } 
          } finally { 
            if (c != null) surfaceHolder.unlockCanvasAndPost(c); 
          } 
        } 
      } 

    } 

    public boolean onTouchEvent(MotionEvent event) { 

     if (event.getAction() != MotionEvent.ACTION_DOWN) return false;  
     if (xDirection!=0 || yDirection!=0)  
      xDirection = yDirection = 0;  
     else {  
      xDirection = (int) event.getX() - xPosition;  
      yDirection = (int) event.getY() - yPosition; 
     }  
     if(ballColor==Color.RED) 
      ballColor=Color.GREEN; 
     else 
      ballColor=Color.RED; 
     return true;  
    } 

    public void setxDirection(int xDirection) { 
     this.xDirection = xDirection; 
    } 

    public void setyDirection(int yDirection) { 
     this.yDirection = yDirection; 
    } 

    public void setballColor(int ballColor) { 
     this.ballColor = ballColor; 
    } 

    public void setradius(int radius) { 
     this.radius = radius; 
    } 
} 

public class SurfaceViewAnimation extends Activity { 
     public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(new BouncingBallView(this,null,0)); 
     } 
} 
+0

어떤 속성을 변경 하시겠습니까? intent.putextra()를 사용할 수 있습니다. –

+0

예를 들어, ballColor를 변경하십시오. 그러나, 나는 그 활동이 생성되거나 "시작"(startActivity)되기 전에 putextra()로만 이것을 변경할 수 있다고 생각합니까? 아닙니다. 활동이 실행될 때 색상을 변경하려면이 방법이 효과가 없다고 생각하십시오. – crossmax

답변

0

가 실행 중인지 SurfaceViewAnimationSurfaceViewAnimation 동안의 데이터를 변경하려는 경우, 당신은 자신의 핸들러를 만드는 것이 가장 좋은 선택입니다.

이 외에도 Handler 초, 정적 변수 또는 Observer 패턴을 사용하여 기본 Inter (프로세스/활동) 통신으로 폴백 할 수 있습니다.

+0

정적 변수 또는 관찰자 패턴을보다 구체적으로 설명 할 수 있습니까? 나는 쉬운 해결책을 찾고 해결책이 최선이 아니라는 것에 신경 쓰지 않는다. (더 효율적이거나 "좋은 연습") – crossmax

+0

이제 주된 활동 (내가 메시지를 받는다)과 SurfaceViewAnimation 클래스 사이에 Handler를 추가하려고한다. 그러나, 내가 SurfaceVoewAnimation 생성자를 직접 호출하지 않고 startActivity (new Intent (this, SurfaceViewAnimation.class))를 통해 동일한 핸들러를 추가하는 방법 ??? – crossmax

관련 문제