Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to listen to Clipboard copy when App is in background

$
0
0

I want to listen to any copy activity to clipboard when app is onPause(), Then retrieve the word copied in an Entry field. I tried this but app crashes when I put it in the background:
in the App.xaml.cs class ..onPause() calling RegPrimaryClipChanged() method :

 public partial class App : Application
    {
        static bool bHasClipChangedListener = false;
        ClipboardManager ClipBoardManager  = (ClipboardManager) Forms.Context.GetSystemService(Context.ClipboardService);
        public App()
        {
            InitializeComponent();

            MainPage = new Translator.MainPage();
        }
static bool bHasClipChangedListener = false;
    ClipboardManager ClipBoardManager  = (ClipboardManager) Forms.Context.GetSystemService(Context.ClipboardService);
protected override void OnSleep()
        {
            // Handle when your app sleeps
            base.OnSleep();
            RegPrimaryClipChanged();

        }
     private void RegPrimaryClipChanged()
            {

                if (!bHasClipChangedListener)
            {
                    ClipBoardManager.AddPrimaryClipChangedListener(new OnPrimaryClipChangedListener());
                bHasClipChangedListener = true;
            }

     public class OnPrimaryClipChangedListener : Java.Lang.Object, ClipboardManager.IOnPrimaryClipChangedListener
            {
                ClipboardManager myClipBoard;

                public void onPrimaryClipChanged()
                {
                    ClipData clipData = myClipBoard.PrimaryClip;
                    //Toast.MakeText(Application.Current, 1, ToastLength.Short).Show();
                    ClipData.Item item = clipData.GetItemAt(0);
                    Translator.MainPage.likecount.Text = item.Text;
                }

                public void OnPrimaryClipChanged()
                {
                    throw new NotImplementedException();
                }
            }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>