title: android - Cannot resolve method getSystemService issue
body: <p>I'm experiencing the problem concerning getSystemService method, as the Android Studio IDE keeps telling me it just cannot resolve this method. As far as I've read it might come out from the fact, that my class doesn't contain activity. I was reading related topics about getSystemService issue, but I couldn't find any matching solution at all.</p>
  
  <p>I place my code above</p>
  
  <pre><code>import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.graphics.Matrix;
  import android.graphics.Rect;
  import android.hardware.Camera;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.TextView;
  
  public class MainActivity extends AppCompatActivity {
  
  private Camera mCamera;
  private CameraView mCameraView;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }
  
  
  @Override
  protected void onResume(){
      super.onResume();
  
      try{
          mCamera = Camera.open(1);
          mCameraView = new CameraView(this, mCamera);
          setContentView(mCameraView);
      } catch (Exception e){
          finish();
      }
  }
  
  @Override
  protected void onPause(){
      if(mCamera != null){
          mCamera.release();
          mCamera = null;
      }
      super.onPause();
  
  
     }
  
  }
  </code></pre>
  
  <p>The bold one of the class above is the one I'm trying to figure out</p>
  
  <pre><code>import android.content.Context;
  import android.hardware.Camera;
  import android.hardware.Camera.Parameters;
  import android.util.Log;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.view.View;
  import android.view.Surface;
  import android.view.Display;
  import android.view.WindowManager;
  import java.io.IOException;
  import android.app.Activity;
  import java.util.List;
  
  
  public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
  
  
  private Camera mCamera;
  private View mView;
  private WindowManager display;
  
  
  public CameraView(Context context, Camera mCamera) {
      super(context);
  
      this.mCamera = mCamera;
      mCamera.setDisplayOrientation(90);
  
      SurfaceHolder holder = getHolder();
      holder.addCallback(this);
      holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  
  }
  
  
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
      try{
          mCamera.setPreviewDisplay(holder);
          mCamera.startPreview();
      } catch (IOException e) {
          Log.e("cameraPreview", "The failure of the camera settings");
      }
  }
  
  
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
      Camera.Parameters params = mCamera.getParameters();
      List&lt;Camera.Size&gt; sizes = params.getSupportedPreviewSizes();
      Camera.Size optionalSize = getOptimalPreviewSize(sizes, width, height);
      params.setPreviewSize(optionalSize.width, optionalSize.height);
      mCamera.setParameters(params);
  
      boolean isPreviewRunning = true;
  
      if (isPreviewRunning)
      {
          mCamera.stopPreview();
      }
  
      Parameters parameters = mCamera.getParameters();
      Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();  
  
      if(display.getRotation() == Surface.ROTATION_0)
      {
          parameters.setPreviewSize(height, width);
          mCamera.setDisplayOrientation(90);
      }
  
      if(display.getRotation() == Surface.ROTATION_90)
      {
          parameters.setPreviewSize(width, height);
      }
  
      if(display.getRotation() == Surface.ROTATION_180)
      {
          parameters.setPreviewSize(height, width);
      }
  
      if(display.getRotation() == Surface.ROTATION_270)
      {
          parameters.setPreviewSize(width, height);
          mCamera.setDisplayOrientation(180);
      }
  
      mCamera.setParameters(parameters);
      previewCamera();
  }
  
  public void previewCamera()
  {
      try
      {
          mCamera.setPreviewDisplay();
          mCamera.startPreview();
          boolean isPreviewRunning = true;
      }
      catch(Exception e)
      {
          Log.d(APP_CLASS, "Cannot start preview", e);
      }
  }
  
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
      mCamera.release();
      mCamera = null;
  }
  
  private Camera.Size getOptimalPreviewSize(List&lt;Camera.Size&gt; sizes, int w, int h) {
  
      final double ASPECT_TOLERANCE = 0.1;
      double targetRatio=(double)h / w;
  
      if (sizes == null) return null;
  
      Camera.Size optimalSize = null;
      double minDiff = Double.MAX_VALUE;
  
      int targetHeight = h;
  
      for (Camera.Size size : sizes) {
          double ratio = (double) size.width / size.height;
          if (Math.abs(ratio - targetRatio) &gt; ASPECT_TOLERANCE) continue;
          if (Math.abs(size.height - targetHeight) &lt; minDiff) {
              optimalSize = size;
              minDiff = Math.abs(size.height - targetHeight);
          }
      }
  
      if (optimalSize == null) {
          minDiff = Double.MAX_VALUE;
          for (Camera.Size size : sizes) {
              if (Math.abs(size.height - targetHeight) &lt; minDiff) {
                  optimalSize = size;
                  minDiff = Math.abs(size.height - targetHeight);
              }
          }
      }
      return optimalSize;
  
  
    }
  
  
  
  }
  </code></pre>
  
  <p>Could You possibly tell me how it should be done to avoid this matter?</p>
  
  <p>Thanks!</p>