Alert Dialog from Android Service
How Do I Display Dialog from a Service? 1 7 Answers Another Way Without Using an Activity: Alertdialog alertDialog = New Alertdialog. Builder(This)...
How do I display dialog from a service?
7 Answers
Another way without using an activity:
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Are you sure?")
.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
Please note that you have to use this permission:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
android-smspopup does exactly that.
A service receives a sms and it starts an Activity with:
android:theme="@android:style/Theme.Dialog"
EDIT: The dialog activity is started here with this code
private void notifyMessageReceived(SmsMmsMessage message) {
(...)
context.startActivity(message.getPopupIntent());
(...)
}
With getPopupIntent() declared as followed (code here):
public Intent getPopupIntent() {
Intent popup = new Intent(context, SmsPopupActivity.class);
popup.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
popup.putExtras(toBundle());
return popup;
}
SmsPopupActivity class obviously defines the dialog activity. Its declared as followed in AndroidManifest.xml:
<activity
android:name=".ui.SmsPopupActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleTask"
android:screenOrientation="user"
android:taskAffinity="net.everythingandroid.smspopup.popup"
android:theme="@style/DialogTheme" >
</activity>
Material styled Dialog from a Service
From a Service, you can easily show a Material Design styled Dialog manipulating its Window type, attributes and LayoutParams.
Must Read
Before we begin: AppCompat Library
This guide presumes you are using Android AppCompat libray.