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();
}
}