即使在隐藏对话框后,对话框也会弹出

时间:2017-10-20 11:14:54

标签: java android lockscreen

我已经实现了一个锁屏,其中有一个检查按钮可以打开像UI这样的计算器,当用户输入他的秘密时,这将解锁完整的锁屏,其中计算器是我的锁屏上的对话框。

提供计算器UI的服务类:

 public class caclservice extends Service {
private int[] numericButtons = {R.id.btnZero, R.id.btnOne, R.id.btnTwo, R.id.btnThree, R.id.btnFour, R.id.btnFive, R.id.btnSix, R.id.btnSeven, R.id.btnEight, R.id.btnNine};
private static final String TAG = PopupService.class.getSimpleName();
WindowManager mWindowManager;
QuestionAdapter qa;
View mView; String username;
TextView ques;
private Session session;
String question;
private TextView txtScreen;
// Represent whether the lastly pressed key is numeric or not
private boolean lastNumeric;
// Represent that current state is in error or not
private boolean stateError;
// If true, do not allow to add another DOT
private boolean lastDot;
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    session=new Session(this);
    qa=new QuestionAdapter(this);
    username=session.getusename();
    question=qa.fetchreco(username);
    showDialog();
    setNumericOnClickListener();
    return super.onStartCommand(intent, flags, startId);
}

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

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

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

    LinearLayout dialog = (LinearLayout) mView.findViewById(R.id.lin);
   // 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);
   txtScreen = (TextView) mView.findViewById(R.id.txtScreen);
    // ques.setText(question);

    // final EditText etMassage = (EditText) mView.findViewById(R.id.ans);
    //etMassage.setText("");
   /* ImageButton imageButtonSend = (ImageButton) mView.findViewById(R.id.imageButtonSendInPopupMessageReceived);
     //        lp = (LayoutParams) imageButton.getLayoutParams();
     //        lp.topMargin = top - 58;
    //        imageButton.setLayoutParams(lp);
    imageButtonSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    //                mView.setVisibility(View.INVISIBLE);
            if(!etMassage.getText().toString().equals(""))
            {

                etMassage.setText("");
            }
        }
    });*/


    Button close = (Button) mView.findViewById(R.id.btnEqual);
    // close.setText("Cancel");
    close.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String ans = txtScreen.getText().toString();
            if (ans.trim().equals(question.trim())) {
                hideDialog();
                android.os.Process.killProcess(android.os.Process.myPid());
                hideDialog();
               // int flags =  WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
               // getWindow().addFlags(flags);

            }
        }
    });


    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 setNumericOnClickListener() {
    // Create a common OnClickListener
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Just append/set the text of clicked button
            Button button = (Button) v;
            if (stateError) {
                // If current state is Error, replace the error message
            txtScreen.setText(button.getText());
                stateError = false;
            } else {
                // If not, already there is a valid expression so append to it
              txtScreen.append(button.getText());
            }
            // Set the flag
            lastNumeric = true;
        }
    };
    // Assign the listener to all the numeric buttons
    for (int id : numericButtons) {
        mView.findViewById(id).setOnClickListener(listener);
    }
}
private void hideDialog(){
    if(mView != null && mWindowManager != null){
        mWindowManager.removeView(mView);
        mView = null;
    }
}

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

这个类为我提供了这样的用户界面Calculator ui in which = button should unlock the device  这里完全正常,但问题是在解锁此计算器对话框之后再次出现在此前UI in home screen 我不想要的。那么如何避免这个UI一次又一次地弹出。

0 个答案:

没有答案