0

SurfaceVanged() 또는 surfaceDestroyed()와 같이 SurfaceView 내부에서 호출하고 싶은 액티비티 클래스에 정적이 아닌 패키지 전용 메서드가 있습니다.SurfaceView의 Activity에서 package-private 메서드를 호출하려면 어떻게해야합니까?

public class MyActivity extends Activity { 

Camera mCamera = Camera.open(); 
Camera.Parameters parameters = mCamera.getParameters(); 
boolean hasFlash = false; 
... 
    void destroyCamera() { 
    flashOff(); 
    mCamera.stopPreview(); 
    mCamera.release(); 
    mCamera = null; 
} 

void updateCamera() { 
    mCamera.setParameters(parameters); 
    mCamera.startPreview(); 
} 
... 
class CameraView extends SurfaceView implements SurfaceHolder.Callback { 
... 
    public void surfaceDestroyed(SurfaceHolder holder) { 
    destroyCamera(); 
} 
... 

더 좋은 방법이 있나요? 또한 같은 액티비티에 toggleFlash() 메서드가 있으므로 키 수신기를 통해 액티비티 내에서 플래시를 토글 할 수 있습니다.

+0

당신이 요구하는 것을 매우 불분명하다. 당신은'SurfaceView'라고 말하지만, 코드에는 아무 것도 언급되어 있지 않습니다. 더 구체적으로 말하십시오. – Phil

+0

Activity에서 메소드를 호출하려고하는 SurfaceView 클래스를 추가했습니다. 오버로드 된 생성자에서 매개 변수로 액티비티를 전달할 수 있습니까? 제가 묻는 것은 SurfaceView 클래스의 Activity 클래스에서 메서드를 호출하는 가장 좋은 방법입니다. 혼란을 드려 죄송합니다 :) – user2081718

답변

0

가 귀하의 코멘트에 대답하기 위해 - 그래, 당신은 context 대신에 생성자에 Activity을 전달할 수 있습니다 : 다음

SurfaceView view = new SurfaceView(this); 

만큼 당신이 contextActivity입니다 확신합니다, 당신은 그것을 전송할 수 있습니다 약한 참조, 스와 통해

//call inside the view 
((MyActivity) getContext()).methodToCall(); 

다른 옵션은 Activity에 정적 참조를 제공하거나, 정적 활동에 대한 액세스 권한을 얻는 포함 : Activity에 메소드를 호출하는 채널과 같이

public static WeakReference<MyActitivity> getActivity(){ 
    return new WeakReference<MyActivity>(self);// self is a static instance of MyActivity 
} 

당신은 onCreate하거나 기본 생성자에 할당하여 Activity에 대한 정적 참조를 얻을 수 있습니다

private static MyActivity self; 

public MyActivity() 
{ 
    super(); 
    self = this; 
} 
관련 문제