When the phone is locked, Android – displays an alarm dialog from the service
My problem is to display the alarm dialog box from the lock screen of the service. When the phone is unlocked, the display effect is very good. In fact, if the phone is locked, it will only unlock the phone, and the alarm dialog box will appear after the lock. This 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 display the alarm dialog when locked
Edit: after I use Android loop and read this link, I use type_ SYstem_ Overlay instead of type_ SYstem_ Alert. In this case, I can't enter content on EditText or even close the dialog box
resolvent:
Change type to type_ SYstem_ ERROR
change
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYstem_ALERT);
to
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYstem_ERROR);
to update:
TYPE_ SYstem_ Alert window type: system windows, such as low battery alarm. These windows are always at the top of the application window. In multi-user systems, they are displayed only on the windows with users. Constant: 2003 (0x000007d3)
TYPE_ SYstem_ Error - window type: internal system error window, at the top of all possible windows. In multi-user system, it is only displayed on the window with user. Constant value: 2010 (0x000007da)
More information is here