在服务锁定屏幕上显示警告对话框是我的问题。当手机处于解锁状态时,显示效果很好。实际上,如果手机被锁定,它只会解锁手机,锁定后会出现警告对话框。这是我的代码:
Service.java:
public static void popupDialog(String sender , String msg)
{
final String senderName = sender;
final String message = msg;
Handler h = new Handler(context.getMainLooper());
h.post(new Runnable() {
@Override
public void run() {
KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (km.inKeyguardRestrictedInputMode())
{
lockFlag = true;
Log.d ("---popup","lock");
powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = km.newKeyguardLock("com.example.myapplication");
mKeyguardLock.disableKeyguard();
wl = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "com.example.myapplication");
wl.acquire();
}
else
{
Log.d ("---popup","unlock");
}
final View view = View.inflate(context.getApplicationContext(),R.layout.popup, null);
final AlertDialog.Builder builder1 = new AlertDialog.Builder(context)
.setCancelable(true)
.setView(view);
final ImageView ImageView1 = (ImageView) view.findViewById(R.id.ImageView1);
final TextView TextView1 = (TextView) view.findViewById(R.id.TextView1);
final TextView TextViewSender = (TextView) view.findViewById(R.id.TextViewSender);
TextViewSender.setText (senderName+":");
final TextView TextView2 = (TextView) view.findViewById(R.id.TextView2);
TextView2.setText (message);
final EditText EditText1 = (EditText) view.findViewById(R.id.EditText1);
final ImageButton ImageButton1 = (ImageButton) view.findViewById(R.id.ImageButton1);
ImageButton1.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
/*do some task*/
}
}
});
AlertDialog alertDialog = builder1.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
}
});
}
我只想在锁定时显示警告对话框。
已编辑:我使用Android Lolipop,在阅读this link后,我使用了 TYPE_SYSTEM_OVERLAY 而不是 TYPE_SYSTEM_ALERT 。在这种情况下,我无法输入EditText甚至关闭对话框。
答案 0 :(得分:3)
将类型更改为TYPE_SYSTEM_ERROR
更改
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
到
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
<强>更新强>
TYPE_SYSTEM_ALERT
- 窗口类型:系统窗口,例如低功率警报。这些窗口始终位于应用程序窗口的顶部。在多用户系统中,仅在拥有用户的窗口中显示。
常数值:2003(0x000007d3)
TYPE_SYSTEM_ERROR
- 窗口类型:内部系统错误窗口,显示在他们可以做的所有事情之上。在多用户系统中,仅在拥有用户的窗口中显示。
常数值:2010(0x000007da)
更多信息是here