Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

ClipboardManager IOnPrimaryClipChangedListener

MarcoTMarcoT USMember ✭✭

Hi,
I'm trying to port a mine app from native Android to Xamarin.
I have to set up a listner for the clipboard so that i can know whan a user copy a text.

Under Android i have this code:

ClipboardManager cliboardManager;
OnPrimaryClipChangedListener ls;
...
ls = new OnPrimaryClipChangedListener() {

                @Override
                public void onPrimaryClipChanged() {
        ...
            };

            cliboardManager
                    .addPrimaryClipChangedListener(ls);

Now my problem is with this code. now I have:

static ClipboardManager cliboardManager;
ClipboardManager.IOnPrimaryClipChangedListener ls;

cliboardManager = (ClipboardManager)GetSystemService(ClipboardService);

but if I try to add the code:

ls = new IOnPrimaryClipChangedListener() {
...
}

VS tell me that I cannot create an instance of an Interface. How can I do?

Thanks

Posts

  • CheesebaronCheesebaron DKInsider, University mod

    You need to implement IOnPrimaryClipChangedListener in a class like:

    public class MyClipChangedListener : Java.Lang.Object, IOnPrimaryClipChangedListener
    {
    
    }
    
  • JoshuaLatusiaJoshuaLatusia USMember ✭✭

    Thankyou @Cheesebaron , took me a while. Since I can't find much info about how to use the Clipboard manager I'm gonna share how I used this Listener.

    First you need to retrieve the ClipboardManager by doing this somewhere in your Android project.
    ClipboardManager clipboardManager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService);

    Then you need to add the A listener to the ClipboardManager this can be achieved by doing this.
    clipboardManager.AddPrimaryClipChangedListener(new OnPrimaryClipChangedListener());

    But to make this code work you need to create your own Listener class which inherits from Both Java.Lang.Object as from IOnPrimaryClipChangedListener. I don't know why you need Java.Lang.Object but otherwise, it doesn't do the job.
    So an example of this class is as follow.

    /// <summary>
    /// Listener when the clipboard of the Android device changes.
    /// </summary>
    public class OnPrimaryClipChangedListener : Java.Lang.Object, ClipboardManager.IOnPrimaryClipChangedListener
    {
    
        /// <summary>
        /// Callback that gets invoked when the primary clip of the device changes.
        /// </summary>
        public void OnPrimaryClipChanged()
        {
            Vibrator v = (Vibrator)Forms.Context.GetSystemService(Context.VibratorService);
            // Vibrate for 5 seconds
            v.Vibrate(5000);
        }
    }
    

    This will make the device vibrate 5 seconds when the primary clip changes, you could replace the code in the OnPrimaryClipChanged function with your own code on what to do.

    Hope this helps other people in the future!

  • asalhiasalhi Member ✭✭
    edited March 2018

    Hey @JoshuaLatusia , How can you achieve this when the app is in background, OnPause() The app stops workings when I apply this code :
    public partial class App : Xamarin.Forms.Application
    {

            public App()
            {
                InitializeComponent();
    
                MainPage = new Translator.MainPage();
            }
    
            protected override void OnStart()
            {
    
                // Handle when your app starts
    
            }
    
    
        protected override void OnSleep()
            {
                // Handle when your app sleeps
                base.OnSleep();
                RegPrimaryClipChanged();
    
            }
             private void RegPrimaryClipChanged()
            {
    
                ClipboardManager clipboardManager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService);
                clipboardManager.AddPrimaryClipChangedListener(new OnPrimaryClipChangedListener());
    
    
            }
    
    
            protected override void OnResume()
            {
                // Handle when your app resumes
            }
        }
    

    And then the Listener Class:

         public class OnPrimaryClipChangedListener : Java.Lang.Object, ClipboardManager.IOnPrimaryClipChangedListener
            {
    
                /// <summary>
                /// Callback that gets invoked when the primary clip of the device changes.
                /// </summary>
                public void OnPrimaryClipChanged()
                {
                    var activity = Xamarin.Forms.Forms.Context as Context;
    
                    Notification.Builder builder = new Notification.Builder(activity)
                             .SetContentTitle("Translator Notification")
                             .SetContentText("Click here to translate your word")
                             .SetSmallIcon(Resource.Drawable.ic_mr_button_grey);
    
                    Notification notification = builder.Build();
    
                    // Get the notification manager:
                    NotificationManager notificationManager = Forms.Context.GetSystemService(Context.NotificationService) as NotificationManager;
    
                    // Publish the notification:
                    const int notificationId = 0;
                    notificationManager.Notify(notificationId, notification);
                }
            }
    
Sign In or Register to comment.