如何显示从后台活动启动的对话框

Nes*_*tor 2 android android-dialog

我的应用程序在后台,我使用的服务在满足某个条件时发送广播:

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

我的广播接收器获得广播,它应该显示一个警告对话框.我用这个:

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);
    }
  }
Run Code Online (Sandbox Code Playgroud)

当我的主要活动位于前台时,警报会正确显示.当它在后台时,什么都看不见.

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);
Run Code Online (Sandbox Code Playgroud)

我尝试使用Dialog主题活动而不是对话框,但它使活动聚焦(我只需要对话框).由于我需要在锁定的屏幕上显示,我在对话框中添加了一些标志,以及以下权限:

 <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"/>
Run Code Online (Sandbox Code Playgroud)

我在Android 2.3.6上测试过它.

我应该设置什么才能使对话框可见?

Nes*_*tor 5

根据您的回答和一些搜索,我得到了这个结果(可能不符合Google UI准则):

创建表示警报的活动

public class DialogMessageActivity extends Activity
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml 中将其主题设置为Theme.Translucent

 android:theme="@android:style/Theme.Translucent"
Run Code Online (Sandbox Code Playgroud)

onCreate中删除其setContentView函数

super.onCreate(savedInstanceState);
//setContentView(R.layout.dialog_message);
Run Code Online (Sandbox Code Playgroud)

添加AlertDialog并从onCreate调用它的show

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();
  }
Run Code Online (Sandbox Code Playgroud)

从任何其他活动(前台或后台活动,无论如何)调用它时,请使用以下标志:

 Intent intent=new Intent(this,DialogMessageActivity.class);
 intent.putExtra(Tags.MESSAGE,msg);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

通过这些步骤,您可以创建一个看似与主应用程序分离并自行显示的对话框.