Someone Of God
Someone Of God

Reputation: 23

Android show user yes-no dialog from background app or service

i want to pop-up a dialog while some specific event occurs. Say i am an antivirus and found a virus, i can't send notification for this right, user could be compromised even dead until he checks the notification.

So i want to show a dialog like allow-deny, or yes-no, not to disturb user, it's gonna happen very rarely.

So is there any way that a service or application shows a dialog while user is using another application, even playing a game.

Thanks in advance...

Upvotes: 1

Views: 939

Answers (1)

StuStirling
StuStirling

Reputation: 16211

Have a look at the Notification documentation. I don't believe you can/should create an AlertDialog when its not in the foreground. See here for Android's information on design patterns regarding when you should and shouldn't use them (you're interested in the last paragraph.

To quote the documentation.

Dialogs and toasts are for feedback not notification

Your app should not create a dialog or toast if it is not currently on screen. Dialogs and Toasts should only be displayed as the immediate response to the user taking an action inside of your app. For further guidance on the use of dialogs and toasts, refer to Confirming & Acknowledging.

This is a better, less intrusive way of notifying the user in your specific case. I also know that its possible to create a notification from a background service.

You create the Notification using the Notification.Builder class, setting things like title, icon, text, when to display it and also the intent to launch when the user clicks on the notification (used to open your app at a desired location). Once created you then use the startForeground(NOTIFICATION_ID,notification). You specify an id so you can access the notification later.

Upvotes: 2

Related Questions