Question

My application is in the background and I use a service that sends a broadcast when a certain condition is met:

if(send)
{
Intent intent = new Intent(Tags.MSG_BROADCAST_ID);
intent.putExtra(Tags.MESSAGE, msg);
Log.w(Tags.DEBUG,"Broadcast");
sendBroadcast(intent);
}
}

My broadcast receiver gets the broadcast and it should show an alert dialog. I use this:

public void onReceive(Context context, Intent intent)
  {
    Bundle bundle = intent.getExtras();   
    if (bundle != null && Globals.MainActivity != null)
    {
      msg = bundle.getString(Tags.MESSAGE);
      Globals.MainActivity.ShowMessage(msg);
    }
  }

When my main activity is in the foreground, the alert is shown correctly. When it is in the background, nothing is visible.

Runnable runnable = new Runnable()
    {
      public void run()
      {
        KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("My_App");
        kl.disableKeyguard();
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "My_App");
        wl.acquire();

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                MainActivity.this);

        // set title
        alertDialogBuilder.setTitle("Your Title");

        // set dialog message
        alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    dialog.dismiss();
                  }
                }
                );

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();
           alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.getWindow().setType(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        // show it
        alertDialog.show();
        wl.release();
      }
    };
    runOnUiThread(runnable);

I tried to use a Dialog themed activity instead of the dialog, but it brings the activity to focus (I only need the dialog). As I need to be shown over locked screen, I added some flag to the dialog, and the following permissions:

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

I tested it on Android 2.3.6.

What should I set to make my dialog visible?

Was it helpful?

Solution

Based on your answers and some searching, I get this result (may not comply with Google UI guidelines):

Create an activity that represents the alert

public class DialogMessageActivity extends Activity

Set its theme to Theme.Translucent in AndroidManifest.xml

 android:theme="@android:style/Theme.Translucent"

Remove its setContentView function in onCreate

super.onCreate(savedInstanceState);
//setContentView(R.layout.dialog_message);

Add an AlertDialog and call its show from onCreate

super.onCreate(savedInstanceState);
//setContentView(R.layout.dialog_message);
displayAlert(msg);



private void displayAlert(String msg)
  {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(msg).setCancelable(
            false).setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                finish();
              }
            });
    AlertDialog alert = builder.create();
    alert.show();
  }

When calling it from any other activity (foreground or background ones, no matter), use the following flag:

 Intent intent=new Intent(this,DialogMessageActivity.class);
 intent.putExtra(Tags.MESSAGE,msg);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

With these steps you can create a dialog that is seemingly separated from the main application and displayed on its own.

OTHER TIPS

The problem is you cant show alert dialog without any user interface, the quick solution is to show it when your application back to front in onResume() or as notification, check that for more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top