本文介绍了手机锁定时,Android-从服务显示警报对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从服务的锁定屏幕上显示警报对话框是我的问题.手机处于解锁状态时,显示效果很好.实际上,如果电话处于锁定状态,它将仅解锁电话,并且警报对话框将出现在锁定后面.这是我的代码:

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.

:我使用Android Lolipop,并在阅读时,我使用了 TYPE_SYSTEM_OVERLAY 而不是 TYPE_SYSTEM_ALERT .在这种情况下,我无法在EditText上输入内容,甚至无法关闭对话框.

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.

推荐答案

将类型更改为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_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 -窗口类型:内部系统错误窗口,显示在所有可能的顶部.在多用户系统中,仅在拥有用户的窗口上显示.恒定值:2010(0x000007da)

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)

更多信息是此处

这篇关于手机锁定时,Android-从服务显示警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:23