Android - 当 phone 被锁定时显示来自服务的警告对话框
Android -showing alert dialog from service when phone is locked
我的问题是在服务的锁定屏幕上显示警告对话框。当 phone 处于解锁状态时,它显示得很好。实际上,如果 phone 是 lock ,它只会解锁 phone 并且在锁定后会出现警告对话框。这是我的代码:
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
- Window类型:系统window,比如低电量提示。这些 windows 始终位于应用程序 windows 之上。在多用户系统中,仅在拥有用户的 window 上显示。
常数值:2003 (0x000007d3)
TYPE_SYSTEM_ERROR
- Window类型:内部系统错误windows,尽其所能出现。在多用户系统中,仅在拥有用户的 window 上显示。
常数值:2010 (0x000007da)
更多信息是here
我的问题是在服务的锁定屏幕上显示警告对话框。当 phone 处于解锁状态时,它显示得很好。实际上,如果 phone 是 lock ,它只会解锁 phone 并且在锁定后会出现警告对话框。这是我的代码:
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
- Window类型:系统window,比如低电量提示。这些 windows 始终位于应用程序 windows 之上。在多用户系统中,仅在拥有用户的 window 上显示。
常数值:2003 (0x000007d3)
TYPE_SYSTEM_ERROR
- Window类型:内部系统错误windows,尽其所能出现。在多用户系统中,仅在拥有用户的 window 上显示。
常数值:2010 (0x000007da)
更多信息是here