Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to display a dialog over a native screen?

I was wondering if anyone can tell if how to pop a dialog screen up over a native Android screen?

I currently have an application that traps an outgoing call and stops it, I then want to pop up a dialog that would take over from the dialler screen and alert the user that there attempt to call has been blocked and allow them have some new options from the dialog.

I know that some people will say that I should use notifications instead but I'm aware of that and its not the way that it should work, I need to be able to pop up a dialog when the call gets trapped.

This is my dialog code so far

  AlertDialog LDialog = new AlertDialog.Builder(context)
     .setTitle("Call Blocked")
     .setMessage("Call Blocked, reroute call?")
     .setPositiveButton("ok", null).create();
      LDialog.show();

I presume I have to somehow get the context to be that of the dialler screen?

Can anyone offer any help and assistance or links to tutorials?

Thanks in advance

like image 614
Donal Rafferty Avatar asked Jan 27 '10 13:01

Donal Rafferty


People also ask

How do I show custom dialog?

This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

For my application I used an activity with the Dialog theme. You can declare the theme in the manifest file :

<activity android:name="PopupActivity"
  android:launchMode="singleInstance" android:excludeFromRecents="true"
  android:taskAffinity="" android:theme="@android:style/Theme.Dialog" />
  • use launcheMode="singleInstance" and taskAffinity="" if your popup is detached from your main application. Otherwise user may click the back button and return to the previous activity of your application.
  • excludeFromRecents="true" to avoid your popup to appear in recent tasks (long press home)
  • theme="@android:style/Theme.Dialog" to set the Dialog theme.
like image 106
tbruyelle Avatar answered Sep 19 '22 08:09

tbruyelle


How to get the equivalent of launchMode = singleTask in code

I have not seen a clear explanation of how to set these flags programmatically, so I will include my results here. tldr: you have to set FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_MULTIPLE_TASK.

If you launch this directly from your app, your dialog will appear on top of your app's last Activity. But if you use a PendingIntent broadcast by AlarmManager to launch your "dialog", you have time to switch to a different app so you can see that your "dialog" will appear over that other app, if the style is set appropriately to show what is behind it.

Obviously one should be responsible about when it is appropriate to display a dialog on top of other apps.

public class MyReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {

// you have to set these flags here where you receive the broadcast
// NOT in the code where you created your pendingIntent
    Intent scheduledIntent = new Intent(context, AlertAlarmActivity.class);
    scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    context.startActivity(scheduledIntent);
like image 44
user1445967 Avatar answered Sep 21 '22 08:09

user1445967