how to write this code in maui android?

mc 5,426 Reputation points
2023-12-26T09:45:27.9966667+00:00
final MediaProjectionManager mediaProjectionManager =  getSystemService(MediaProjectionManager.class);
final MediaProjection[] mediaProjection = new MediaProjection[1];
ActivityResultLauncher<Intent> startMediaProjection = registerForActivityResult(  new StartActivityForResult(),  result -> {    if (result.getResultCode() == Activity.RESULT_OK) {
      mediaProjection[0] = mediaProjectionManager.getMediaProjection(result.getResultCode(), result.getData());    }  });

how to write this in maui?

I use RegisterForActivityResult but the callback method not run. why?

the RegisterForActivityResult code is run but the callback not .

Developer technologies .NET .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-12-27T05:36:22.1266667+00:00

    Hello,

    Please invoke above code in the MainActivity.cs. I used following code and created MyActivityCallback.cs, OnActivityResult method will be executed.

    using Android.App;
    using Android.Content;
    using Android.Content.PM;
    using Android.Media.Projection;
    using Android.OS;
    using AndroidX.Activity.Result;
    using static AndroidX.Activity.Result.Contract.ActivityResultContracts;
    
    
    namespace MauiApp1
    {
        [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
        public class MainActivity : MauiAppCompatActivity
        {
            private MediaProjectionManager mediaProjectionManager;
            private MediaProjection mediaProjection;
           protected override void OnCreate(Bundle? savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
    
               Activity CurrentActivity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity;
               // Initialize MediaProjectionManager
                mediaProjectionManager = (MediaProjectionManager)GetSystemService(Context.MediaProjectionService);
               // Register for activity result
                var startMediaProjection = RegisterForActivityResult(new StartActivityForResult(), new MyActivityCallback(mediaProjectionManager));
               // Start the media projection activity
                Intent mediaProjectionIntent = mediaProjectionManager.CreateScreenCaptureIntent();
                startMediaProjection.Launch(mediaProjectionIntent);
            }
           protected override void OnActivityResult(int requestCode, Result resultCode, Intent? data)
            {
                base.OnActivityResult(requestCode, resultCode, data);
            }
        }
    
    
       internal class MyActivityCallback : Java.Lang.Object, IActivityResultCallback
        {
            private MediaProjectionManager? mediaProjectionManager;
    
    
           public MyActivityCallback(MediaProjectionManager? mediaProjectionManager)
            {
                this.mediaProjectionManager = mediaProjectionManager;
            }
           public void OnActivityResult(Java.Lang.Object? result)
            {
                ActivityResult activityResult= result as ActivityResult;
                if (activityResult.ResultCode == -1) {
                   MediaProjection mediaProjection = mediaProjectionManager.GetMediaProjection(activityResult.ResultCode, activityResult.Data);
                }
            }
        }
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.