public override void OnDestroy()
        {
            Kp2aLog.Log("CopyToClipboardService.OnDestroy");

            // These members might never get initialized if the app timed out
            if (_stopOnLockBroadcastReceiver != null)
            {
                UnregisterReceiver(_stopOnLockBroadcastReceiver);
                _stopOnLockBroadcastReceiver = null;
            }
            if (_notificationDeletedBroadcastReceiver != null)
            {
                UnregisterReceiver(_notificationDeletedBroadcastReceiver);
                _notificationDeletedBroadcastReceiver = null;
            }
            if (_notificationManager != null)
            {
                _notificationManager.Cancel(NotifyPassword);
                _notificationManager.Cancel(NotifyUsername);
                _notificationManager.Cancel(NotifyKeyboard);
                _notificationManager.Cancel(NotifyCombined);

                _numElementsToWaitFor = 0;
                ClearKeyboard(true);
            }
            if (_clearClipboardTask != null)
            {
                Kp2aLog.Log("Clearing clipboard due to stop CopyToClipboardService");
                _clearClipboardTask.Run();
            }

            Kp2aLog.Log("Destroyed Show-Notification-Receiver.");

            base.OnDestroy();
        }
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            Kp2aLog.Log("Received intent to provide access to entry");

            _stopOnLockBroadcastReceiver = new StopOnLockBroadcastReceiver(this);
            IntentFilter filter = new IntentFilter();

            filter.AddAction(Intents.DatabaseLocked);
            RegisterReceiver(_stopOnLockBroadcastReceiver, filter);

            if ((intent.Action == Intents.ShowNotification) || (intent.Action == Intents.UpdateKeyboard))
            {
                String uuidBytes = intent.GetStringExtra(EntryActivity.KeyEntry);
                String searchUrl = intent.GetStringExtra(SearchUrlTask.UrlToSearchKey);

                PwUuid entryId = PwUuid.Zero;
                if (uuidBytes != null)
                {
                    entryId = new PwUuid(MemUtil.HexStringToByteArray(uuidBytes));
                }

                PwEntryOutput entry;
                try
                {
                    if ((App.Kp2a.GetDb().LastOpenedEntry != null) &&
                        (entryId.Equals(App.Kp2a.GetDb().LastOpenedEntry.Uuid)))
                    {
                        entry = App.Kp2a.GetDb().LastOpenedEntry;
                    }
                    else
                    {
                        entry = new PwEntryOutput(App.Kp2a.GetDb().Entries[entryId], App.Kp2a.GetDb().KpDatabase);
                    }
                }
                catch (Exception)
                {
                    //seems like restarting the service happened after closing the DB
                    StopSelf();
                    return(StartCommandResult.NotSticky);
                }

                if (intent.Action == Intents.ShowNotification)
                {
                    //first time opening the entry -> bring up the notifications
                    bool closeAfterCreate = intent.GetBooleanExtra(EntryActivity.KeyCloseAfterCreate, false);
                    DisplayAccessNotifications(entry, closeAfterCreate, searchUrl);
                }
                else                 //UpdateKeyboard
                {
#if !EXCLUDE_KEYBOARD
                    //this action is received when the data in the entry has changed (e.g. by plugins)
                    //update the keyboard data.
                    //Check if keyboard is (still) available
                    if (Keepass2android.Kbbridge.KeyboardData.EntryId == entry.Uuid.ToHexString())
                    {
                        MakeAccessibleForKeyboard(entry, searchUrl);
                    }
#endif
                }
            }
            if (intent.Action == Intents.CopyStringToClipboard)
            {
                TimeoutCopyToClipboard(intent.GetStringExtra(_stringtocopy));
            }
            if (intent.Action == Intents.ActivateKeyboard)
            {
                ActivateKp2aKeyboard();
            }
            if (intent.Action == Intents.ClearNotificationsAndData)
            {
                ClearNotifications();
            }


            return(StartCommandResult.RedeliverIntent);
        }
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            Kp2aLog.Log("Received intent to provide access to entry");

            _stopOnLockBroadcastReceiver = new StopOnLockBroadcastReceiver(this);
            IntentFilter filter = new IntentFilter();
            filter.AddAction(Intents.DatabaseLocked);
            RegisterReceiver(_stopOnLockBroadcastReceiver, filter);

            if ((intent.Action == Intents.ShowNotification) || (intent.Action == Intents.UpdateKeyboard))
            {
                String uuidBytes = intent.GetStringExtra(EntryActivity.KeyEntry);

                PwUuid entryId = PwUuid.Zero;
                if (uuidBytes != null)
                    entryId = new PwUuid(MemUtil.HexStringToByteArray(uuidBytes));

                PwEntryOutput entry;
                try
                {
                    if ((App.Kp2a.GetDb().LastOpenedEntry != null)
                        && (entryId.Equals(App.Kp2a.GetDb().LastOpenedEntry.Uuid)))
                    {
                        entry = App.Kp2a.GetDb().LastOpenedEntry;
                    }
                    else
                    {
                        entry = new PwEntryOutput(App.Kp2a.GetDb().Entries[entryId], App.Kp2a.GetDb().KpDatabase);
                    }

                }
                catch (Exception)
                {
                    //seems like restarting the service happened after closing the DB
                    StopSelf();
                    return StartCommandResult.NotSticky;
                }

                if (intent.Action == Intents.ShowNotification)
                {
                    //first time opening the entry -> bring up the notifications
                    bool closeAfterCreate = intent.GetBooleanExtra(EntryActivity.KeyCloseAfterCreate, false);
                    DisplayAccessNotifications(entry, closeAfterCreate);
                }
                else //UpdateKeyboard
                {
            #if !EXCLUDE_KEYBOARD
                    //this action is received when the data in the entry has changed (e.g. by plugins)
                    //update the keyboard data.
                    //Check if keyboard is (still) available
                    if (Keepass2android.Kbbridge.KeyboardData.EntryId == entry.Uuid.ToHexString())
                        MakeAccessibleForKeyboard(entry);
            #endif
                }
            }
            if (intent.Action == Intents.CopyStringToClipboard)
            {

                TimeoutCopyToClipboard(intent.GetStringExtra(_stringtocopy));
            }
            if (intent.Action == Intents.ActivateKeyboard)
            {
                ActivateKp2aKeyboard();
            }
            if (intent.Action == Intents.ClearNotificationsAndData)
            {
                ClearNotifications();
            }

            return StartCommandResult.RedeliverIntent;
        }
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            Kp2aLog.Log("Received intent to provide access to entry");

            if (_stopOnLockBroadcastReceiver == null)
            {
                _stopOnLockBroadcastReceiver = new StopOnLockBroadcastReceiver(this);
                IntentFilter filter = new IntentFilter();
                filter.AddAction(Intents.DatabaseLocked);
                RegisterReceiver(_stopOnLockBroadcastReceiver, filter);
            }

            if ((intent.Action == Intents.ShowNotification) || (intent.Action == Intents.UpdateKeyboard))
            {
                String entryId   = intent.GetStringExtra(EntryActivity.KeyEntry);
                String searchUrl = intent.GetStringExtra(SearchUrlTask.UrlToSearchKey);

                if (entryId == null)
                {
                    Kp2aLog.Log("received intent " + intent.Action + " without KeyEntry!");
#if DEBUG
                    throw new Exception("invalid intent received!");
#endif
                    return(StartCommandResult.NotSticky);
                }


                PwEntryOutput entry;
                try
                {
                    ElementAndDatabaseId fullId = new ElementAndDatabaseId(entryId);


                    if (((App.Kp2a.LastOpenedEntry != null) &&
                         (fullId.ElementId.Equals(App.Kp2a.LastOpenedEntry.Uuid))))
                    {
                        entry = App.Kp2a.LastOpenedEntry;
                    }
                    else
                    {
                        Database entryDb = App.Kp2a.GetDatabase(fullId.DatabaseId);
                        entry = new PwEntryOutput(entryDb.EntriesById[fullId.ElementId], entryDb);
                    }
                }
                catch (Exception e)
                {
                    Kp2aLog.LogUnexpectedError(e);
                    //seems like restarting the service happened after closing the DB
                    StopSelf();
                    return(StartCommandResult.NotSticky);
                }

                if (intent.Action == Intents.ShowNotification)
                {
                    //first time opening the entry -> bring up the notifications
                    bool activateKeyboard = intent.GetBooleanExtra(EntryActivity.KeyActivateKeyboard, false);
                    DisplayAccessNotifications(entry, activateKeyboard, searchUrl);
                }
                else //UpdateKeyboard
                {
#if !EXCLUDE_KEYBOARD
                    //this action is received when the data in the entry has changed (e.g. by plugins)
                    //update the keyboard data.
                    //Check if keyboard is (still) available
                    if (Keepass2android.Kbbridge.KeyboardData.EntryId == entry.Uuid.ToHexString())
                    {
                        MakeAccessibleForKeyboard(entry, searchUrl);
                    }
#endif
                }
            }
            if (intent.Action == Intents.CopyStringToClipboard)
            {
                TimeoutCopyToClipboard(intent.GetStringExtra(_stringtocopy));
            }
            if (intent.Action == Intents.ActivateKeyboard)
            {
                ActivateKp2aKeyboard();
            }
            if (intent.Action == Intents.ClearNotificationsAndData)
            {
                ClearNotifications();
            }


            return(StartCommandResult.RedeliverIntent);
        }