private void StartScreenCapture()
 {
     try
     {
         if (surface == null || Activity == null)
         {
             return;
         }
         if (mediaProjection != null)
         {
             SetUpVirtualDisplay();
         }
         else if (resultCode != 0 && resultData != null)
         {
             SetUpMediaProjection();
             SetUpVirtualDisplay();
         }
         else
         {
             Log.Info(TAG, "Requesting confirmation");
             // This initiates a prompt for the user to confirm screen projection.
             StartActivityForResult(mediaProjectionManager.CreateScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);
         }
     }
     catch (Exception e)
     {
     }
 }
 private void SetUpVirtualDisplay()
 {
     Log.Info(TAG, "Setting up a VirtualDisplay: " + surfaceView.Width + "x" + surfaceView.Height + " (" + screenDensity + ")");
     virtualDisplay = mediaProjection.CreateVirtualDisplay("ScreenCapture",
                                                           surfaceView.Width, surfaceView.Height, screenDensity,
                                                           (DisplayFlags)VirtualDisplayFlags.AutoMirror, surface, null, null);
     buttonToggle.SetText(Resource.String.stop);
 }
        public override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if (requestCode == REQUEST_MEDIA_PROJECTION)
            {
                if (resultCode != Result.Ok)
                {
                    Log.Info(TAG, "User cancelled");
                    Toast.MakeText(Activity, Resource.String.user_cancelled, ToastLength.Short).Show();
                    return;
                }
                if (Activity == null)
                {
                    return;
                }

                Log.Info(TAG, "Starting screem capture");
                this.resultCode = (int)resultCode;
                this.resultData = data;
                SetUpMediaProjection();
                SetUpVirtualDisplay();
            }
        }