问题
Showing alert dialog on lock screen from service is my problem. when phone is on unlock state ,it shows nicely. Actually if phone is lock , it will just unlock phone and alert dialog will appears behind lock. Here is my code:
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();
}
});
}
I just want to show alert dialog on lock.
EDITED : I use Android Lolipop, and after read this link , I used TYPE_SYSTEM_OVERLAY instead of TYPE_SYSTEM_ALERT. In this situation, I cannot type on EditText or even close dialog.
回答1:
Change the type to TYPE_SYSTEM_ERROR
Change
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
to
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
Update:
TYPE_SYSTEM_ALERT
- 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.
Constant Value: 2003 (0x000007d3)
TYPE_SYSTEM_ERROR
- Window type: internal system error windows, appear on top of everything they can. In multiuser systems shows only on the owning user's window.
Constant Value: 2010 (0x000007da)
More info is here
来源:https://stackoverflow.com/questions/35125925/android-showing-alert-dialog-from-service-when-phone-is-locked