通常咱們都是在Activity中建立dialog的,不管是系統的AlertDialog仍是咱們自定義的Dialog。
咱們只需設置title,message等信息,而後show出來便可。android
但是在服務中呢,沒有activity,dialog沒有了依賴,那麼怎麼辦呢? 這時,dialog就要以系統對話框的形式彈出了。在咱們取得Dialog對象後,需給它設置類型,即:windows
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
1
看看這個TYPE的解釋app
/**
* Window type: system window, such as low power alert. These windows
* are always on top of application windows.
* In multiuser systems shows only on the owning user's window.
*/
public static final int TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3;
在這裏別忘了在Manifest中加上權限ide
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
1
最後來段示例代碼ui
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage("Hello,My name is Message.");
builder.setNegativeButton("CANCEL", null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 具體操做
}
});
final Dialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
.net