psv
psv

Reputation: 3287

Display an alertdialog out of app

I would like to display an alertdialog from a Service even if the user is not currently on my app.

I read on the web that we necessarily need to create another activity with a "Popup theme"...

So also tried this : how to create alertdialog outside of an application?

But it always open my app before showing the new activity.

I am sure there is another solution. What does Facebook messenger use to show some notifs when new message received?

Thanks


EDIT

I just found that: http://www.piwai.info/chatheads-basics/

Let's try it!

EDIT 2

Okay, I tested the trick in my previous edit. It can be a solution, but it is not very clean... I mean, some developers use this to show ads to the user, it can be intrusive, and I need to add a permission which warn at the install time. I finally decided to open my app and display my AlertDialog in my app.

EDIT 3

I solved my problem. See my answer for more information

Upvotes: 0

Views: 1376

Answers (2)

Alexander
Alexander

Reputation: 48252

Proper way would be to display a Notification, not a dialog.

Upvotes: -1

psv
psv

Reputation: 3287

I finally found a solution.

Look at this link: http://jhshi.me/2013/11/03/pop-up-alertdialog-in-system-service/

Add

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

And

AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();

It is exactly what I wanted to do.

NB: I heard that in Android Marshmallow the permission is deactivated by default, and the user will not see the popup if he does not manually activate the permission in the app preferences (apps manager).

I've tested under Marshmallow and I hadn't to force anything in android settings for this app.

Upvotes: 1

Related Questions