手机锁定时,Android-从服务显示警报对话框
作者:互联网
我的问题是从服务的锁定屏幕上显示警报对话框.手机处于解锁状态时,显示效果很好.实际上,如果电话处于锁定状态,它将仅解锁电话,并且警报对话框将出现在锁定后面.这是我的代码:
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上输入内容,甚至无法关闭对话框.
解决方法:
将类型更改为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
标签:android-5-0-lollipop,dialog,java,android,service 来源: https://codeday.me/bug/20191119/2033509.html