锁屏对话框通知,如 whatsapp

lock screen dialog notification like whatsapp

我正在开发像 whatsapp 这样的聊天应用程序,我正在开发锁定屏幕上的弹出通知。我开发了代码,但问题是——我在对话框上使用了两个按钮,比如 whatsapp 作为关闭和查看,但是按钮上的 onclick 侦听器不起作用,因为对话框没有焦点,每当我在背景的任何地方单击对话框时单击屏幕的执行单击。我需要这种类型的对话框和功能,比如 --

但是对话框没有获得焦点或问题是其他的,我无法理解,请帮助。

为了实现这一点,我正在使用如下服务 -

public class PopupService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        Utils.printLog("on bind");
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Utils.printLog("on start command");
        PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
        WakeLock mWakeLock = pm.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "YourServie");
        mWakeLock.acquire();
        mWakeLock.release();

        ///////////////////////////////////

        final WindowManager mWindowManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        final View mView = inflater.inflate(R.layout.dialog_popup_notification_received, null);

        Utils.printLog("view" + mView.isClickable());
        mView.setClickable(true);
        mView.setFocusable(true);
        mView.setFocusableInTouchMode(true);

        mView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Utils.printLog("view" + mView.isClickable());
            }
        });

        TextView tvMessage = (TextView) mView.findViewById(R.id.TextViewMessageInPopupMessageReceived);
        tvMessage.setText(intent.getStringExtra("msg"));
        ImageButton ibSend = (ImageButton) mView.findViewById(R.id.imageButtonSendInPopupMessageReceived);
        TextView tvClose = (TextView) mView.findViewById(R.id.TextViewCloseInPopupMessageReceived);
        TextView tvView = (TextView) mView.findViewById(R.id.textviewViewInPopupMessageReceived);

        ibSend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) 
            {
                Utils.printLog("send");
            }
        });
        tvClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) 
            {
                Utils.printLog("close");
            }
        });
        tvView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) 
            {
                Utils.printLog("view");
            }
        });

        WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
        /* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */,
        PixelFormat.RGBA_8888);

        mWindowManager.addView(mView, mLayoutParams);
        mWindowManager.updateViewLayout(mView, mLayoutParams);

        return super.onStartCommand(intent, flags, startId);
    }

}

您可以使用主题代替 Dialog.As 很好地使用该活动,因为 Dialog.It 可能有效。

我今天得到了解决方案..

这里是我的代码...

public class PopupService extends Service{

    private static final String TAG = PopupService.class.getSimpleName();
    WindowManager mWindowManager;
    View mView;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        showDialog(intent.getStringExtra("msg"));
        return super.onStartCommand(intent, flags, startId);
    }

    private void showDialog(String aTitle)
    {
        PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
        WakeLock mWakeLock = pm.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "YourServie");
        mWakeLock.acquire();
        mWakeLock.release();

        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mView = View.inflate(getApplicationContext(), R.layout.dialog_popup_notification_received, null);
        mView.setTag(TAG);

        int top = getApplicationContext().getResources().getDisplayMetrics().heightPixels / 2;

        LinearLayout dialog = (LinearLayout) mView.findViewById(R.id.pop_exit);
// if you want to set params
//        android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) dialog.getLayoutParams();
//        lp.topMargin = top;
//        lp.bottomMargin = top;
//        mView.setLayoutParams(lp);

        final EditText etMassage = (EditText) mView.findViewById(R.id.editTextInPopupMessageReceived);

        ImageButton imageButtonSend = (ImageButton) mView.findViewById(R.id.imageButtonSendInPopupMessageReceived);
//        lp = (LayoutParams) imageButton.getLayoutParams();
//        lp.topMargin = top - 58;
//        imageButton.setLayoutParams(lp);
        imageButtonSend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.printLog("clicked");
//                mView.setVisibility(View.INVISIBLE);
                if(!etMassage.getText().toString().equals(""))
                {
                    Utils.printLog("sent");
                    etMassage.setText("");
                }
            }
        });

        TextView close = (TextView) mView.findViewById(R.id.TextViewCloseInPopupMessageReceived);
        close.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                hideDialog();   
            }
        });

        TextView view = (TextView) mView.findViewById(R.id.textviewViewInPopupMessageReceived);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                hideDialog();   
            }
        });

        TextView message = (TextView) mView.findViewById(R.id.TextViewMessageInPopupMessageReceived);
        message.setText(aTitle);

        final WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT, 0, 0,
        WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON ,
        PixelFormat.RGBA_8888);

        mView.setVisibility(View.VISIBLE);
        mWindowManager.addView(mView, mLayoutParams);
        mWindowManager.updateViewLayout(mView, mLayoutParams);

    }

    private void hideDialog(){
        if(mView != null && mWindowManager != null){
            mWindowManager.removeView(mView);
            mView = null;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

它对我有用。

我在 link;

找到了解决方案