Joshua Abrams
Joshua Abrams

Reputation: 387

Android: Detect touch from service

I am trying to find a way to detect when a user touches the screen in any way while my services is running in the background. For example, I want to be able to detect when a user presses on an app icon on the home screen. Is this possible and if so how would it be implemented?

Upvotes: 2

Views: 2114

Answers (1)

Jack
Jack

Reputation: 9242

Please see this explanation of services. You should be able to send a message to your service using a Messenger. So on your onClick event you will call your messenger ... example:

        try {
            Message msg = Message.obtain(null,
                    MessengerService.MSG_BUTTON_CLICKED);
            msg.replyTo = mMessenger;
            mService.send(msg);

            // Give it some value as an example.
            msg = Message.obtain(null,
                    MessengerService.MSG_SET_VALUE, this.hashCode(), 0);
            mService.send(msg);
        } catch (RemoteException e) {
            // In this case the service has crashed before we could even
            // do anything with it; we can count on soon being
            // disconnected (and then reconnected if it can be restarted)
            // so there is no need to do anything here.
        }

Upvotes: 1

Related Questions